title: Possible memory leak with malloc, struct, std::string, and free
body: <p>I've a situation like the following, and I'm not sure whether or not the std::string elements of the struct leak memory or if this is ok. Is the memory allocated by those two std::strings deleted when <code>free(v)</code> is called?</p>
  
  <pre><code>struct MyData
  {
      std::string s1;
      std::string s2;
  };
  
  void* v = malloc(sizeof(MyData));
  </code></pre>
  
  <p>...</p>
  
  <pre><code>MyData* d = static_cast&lt;MyData*&gt;(v);
  d-&gt;s1 = "asdf";
  d-&gt;s2 = "1234";
  </code></pre>
  
  <p>...</p>
  
  <pre><code>free(v);
  </code></pre>
  
  <p>Leak or not?</p>
  
  <p>I'm using the void-pointer because I have another superior struct, which consists of an enum and a void-pointer. Depending on the value of the enum-variable, the void* will point to different data-structs.</p>
  
  <p>Example:</p>
  
  <p>enum-field has EnumValue01 => void-pointer will point to a malloc'd MyData01 struct</p>
  
  <p>enum-field has EnumValue02 => void-pointer will point to a malloc'd MyData02 struct</p>
  
  <p>Suggestions for different approaches are very appreciated, of course.</p>