Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>std::string is a class and should not be copied by a memcpy operation. It may hold instance-specific data, which will get scrambled if held by two different instances (and probably that's the cause of your problems).</p> <p>Imagine, that std::string is something like:</p> <pre><code>class string { private: char * data; int dataLength; }; </code></pre> <p>If you memcpy one string to another, both data and dataLength are copied to another place (lately being treated as a normal string instance). However, when the destructor is called on these strings (when they run out of scope), they will attempt to free the data held in <code>data</code> field. First string (which is an actual owner of this pointer) will free memory pointed to by this pointer. But then another destructor of your copied string will run and will try to free this memory again, what is not permitted.</p> <p>Note, that it's exactly, what your system is reporting: double freeing of memory.</p> <p>Your code is very C-style. In C++ one would create a class with constructor rather than a function, which fills in a struct. I would write your code in the following way:</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; struct Person { public: string Name; string Lastname; int Age; Person(string newName, string newLastname, int newAge) : Name(newName), Lastname(newLastname), Age(newAge) { } }; int main(int argc, char *argv[]) { vector&lt;Person&gt; people; Person person1("Zareh", "Petros", 13); people.push_back(person1); Person person2("Tina", "Yarros", 26); people.push_back(person2); for(unsigned int i=0; i &lt; people.size() ; i++) { cout &lt;&lt; "Element - " &lt;&lt; i &lt;&lt; endl; cout &lt;&lt; "name:" &lt;&lt; people[i].Name &lt;&lt; endl; cout &lt;&lt; "lastname:" &lt;&lt; people[i].Lastname &lt;&lt; endl; cout &lt;&lt; "age:" &lt;&lt; people[i].Age &lt;&lt; endl; } getchar(); return 0; } </code></pre> <p>The role of your creation method takes the class constructor. It fills in fields of the class properly. Also, C++ provides default copy-constructor and assignment operator, which will handle assigning one person to another properly.</p> <hr /> <p>Some side notes about your code style.</p> <ul> <li>In C++ avoid using <code>memcpy</code>. If you have a need of using it, you probably should consider creating proper copy constructor, std::copy or simply making an assignment (what would work perfectly in your situation). <code>memcpy</code> should be used <em>only</em> when copying raw chunks of memory.</li> <li><p>typedef is no longer required for structs in C++. Instead of writing:</p> <pre><code>typedef struct Name { ... } Name; </code></pre> <p>You can simply write:</p> <pre><code>struct Name { ... }; </code></pre></li> </ul>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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