title: Pass vector by reference to constructor of class
body: <p>I have a class called <code>test</code> with which I want to associate a large vector with in the order of million elements. I have tried doing this by passing a pointer to the constructor:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  class test{
  public:
      vector&lt;double&gt;* oneVector;
      test(vector&lt;double&gt;* v){
          oneVector = v;
      }
      int nElem(){return oneVector-&gt;size();}
  };
  
  int main(){
      vector&lt;double&gt;  v(1000000);
      cout &lt;&lt; v.size() &lt;&lt; endl;
      vector&lt;double&gt;* ptr;
  
      test t(ptr);
      cout &lt;&lt; t.nElem()&lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p>However, this results in a <code>Segmentation Fault:11</code>, precisely when I try to do <code>t.nElem()</code>. What could be the problem?</p>