Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange error regarding STL vector of pointers
    primarykey
    data
    text
    <p>I've a small code snippet which produces an error on running. The following code stores data of mystruct and creates a vector of pointers which store the addresses of the corresponding data.</p> <pre><code>struct mystruct{ std::vector &lt; int &gt; someinfo; int somenumbers; double *somepointer; double someparm; void Print(){....} }; void DoSomething(mystruct &amp;_somestruct, std::vector&lt; mystruct &gt; &amp;_somevec, std::vector&lt;mystruct *&gt; &amp;_ptr){ _somevec.push_back(_somestruct); _ptr.push_back(&amp;(_somevec.back())); } void ReadStruct(){ std::vector&lt;mystruct &gt; _vec; std::vector&lt;mystruct *&gt; _ptr; for(int i=0;i&lt;100;i++){ mystruct _struct; _struct.somenumbers = 3; _struct.someinfo.push_back(0); _struct.someinfo.push_back(1); _struct.someinfo.push_back(2); DoSomething(_struct, _vec, _ptr); } _vec[0].Print(); //Prints correctly _ptr[0]-&gt;Print();//Prints garbage info } </code></pre> <p>If I first create the vector and then create the vector of pointers then the code works perfectly i.e.</p> <pre><code>void DoSomething(mystruct &amp;_somestruct, std::vector&lt; mystruct &gt; &amp;_somevec){ _somevec.push_back(_somestruct); //_ptr.push_back(&amp;(_somevec.back())); } void DoSomething1(std::vector&lt; mystruct &gt; &amp;_somevec, std::vector&lt;mystruct *&gt; &amp;_ptr){ for(int i=0;i&lt;_somevec.size();i++) _ptr.push_back(&amp;(_somevec[i])); } </code></pre> <p>I do not know what mistake I am doing. Your help/inputs is greatly appreciated!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload