Note that there are some explanatory texts on larger screens.

plurals
  1. POerror pushing structure objects with string members into a std::vector
    primarykey
    data
    text
    <p>I'm a C#, C programmer learning C++ and I'm having a bit of a trouble. I'm trying to push an object of the struct type 'Person' into a vector but the string values which are members of the Person type are not copied. Also the code exits with an error message - posted at the bottom:</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;string&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; using namespace std; typedef struct Person { string Name; string Lastname; int Age; } Person; void CreatePerson(Person* in_person, string in_name, string in_last, int in_age) { Person t_person; t_person.Name = in_name; t_person.Lastname = in_last; t_person.Age = in_age; memcpy(in_person, &amp;t_person, sizeof(t_person)); } int main(int argc, char *argv[]) { vector&lt;Person&gt; people; Person t_ppl; CreatePerson(&amp;t_ppl, "Zareh", "Petros", 13); people.push_back(t_ppl); CreatePerson(&amp;t_ppl, "Tina", "Yarroos", 26); people.push_back(t_ppl); int ii; for(ii=0; ii &lt; people.size() ; ii++) { cout &lt;&lt; "Element - " &lt;&lt; ii &lt;&lt; endl; cout &lt;&lt; "name:" &lt;&lt; people[ii].Name &lt;&lt; endl; cout &lt;&lt; "lastname:" &lt;&lt; people[ii].Lastname &lt;&lt; endl; cout &lt;&lt; "age:" &lt;&lt; people[ii].Age &lt;&lt; endl; } return 0; } </code></pre> <p>And here is the error message:</p> <pre><code>*** glibc detected *** ./a.out: double free or corruption (fasttop): 0x09d48048 *** ======= Backtrace: ========= /lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb74e8ee2] /usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x1f)[0xb76e551f] /usr/lib/i386-linux-gnu/libstdc++.so.6(_ZNSs4_Rep10_M_destroyERKSaIcE+0x1b)[0xb76cc99b] /usr/lib/i386-linux-gnu/libstdc++.so.6(+0x909dc)[0xb76cc9dc] /usr/lib/i386-linux-gnu/libstdc++.so.6(_ZNSsD1Ev+0x2e)[0xb76cca4e] </code></pre>
    singulars
    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.
    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