title: Malloc on a struct containing a std::vector
body: <p>Here is the situation :</p>
  
  <p>I use a malloc to allocate memory for a struct.
  The struct contains various items such as pointers, string variables and vectors.</p>
  
  <p>The fact is, when we use malloc, no constructors are called. Using a code similar to the following one, I've experienced some situation where some variables worked while others didn't.</p>
  
  <p>Note : The following code doesn't compile. It's purpose is only to illustrate the situation.</p>
  
  <pre><code>struct MyStruct 
  {
      MyClass*    mFirstClass;
      bool        mBool;
      std::string mString;
      std::vector&lt;MyClass&gt; mVector;
  };
  
  
  int main()  
  {  
  
      MyStruct* wMyStructure;  
      wMyStructure = (MyStruct*) malloc (sizeof(MyStruct));  
  
      MyClass wMyClassObject;
  
      wMyStructure-&gt;mFirstClass = new MyClass();  
      wMyStructure-&gt;mFirstClass-&gt;func();  
      wMyStructure-&gt;mBool = false;  
      wMyStructure-&gt;mString = "aString";  
      wMyStructure-&gt;mVector.push_back(wMyClassObject);
  
      return 0;
  }
  </code></pre>
  
  <p>By using pointers instead of those variables (<code>std::string* mString</code>), followed by a call to the object constructor (<code>mString = new std::string;</code>) Exception are not thrown.</p>
  
  <p>However, I've experienced a situation where the mString was used without problem without the constructor being called, but when it came to the vector, the application exit automatically.</p>
  
  <p>This left me with many questions:</p>
  
  <ol>
  <li><p>When will an object throw an exception if no constructor were used?   </p></li>
  <li><p>In the situation I experienced, only the vector caused problem. Could mString be left as it is or should I call it's constructor?  </p></li>
  <li><p>What would be the safest way, using malloc, to do the whole thing?</p></li>
  </ol>