title: std::vector new runs out of memory
body: <p>I am writing a program in C++ for a embedded platform STM32. I used to use normal arrays but for the first time I am using vectors in it. So far it runs well in the majority of the cases. However in a very few cases I got the error stated in the title eventhough it shouldn't. Would appreciate some help as I run out of ideas.</p>
  
  <p><strong>My situation</strong></p>
  
  <p>The vectors are like this</p>
  
  <pre><code>struct Boundary{
    vector&lt;unsigned short&gt; x; //The x coordinates
    vector&lt;unsigned short&gt; y; //The y coordinates  
  };
  </code></pre>
  
  <p>Everytime I use these vectors I clear them with </p>
  
  <pre><code>boundary0.x.clear();
  boundary0.y.clear();
  </code></pre>
  
  <p>I add elements with the normal push_back</p>
  
  <p><strong>The strange part</strong></p>
  
  <p>Sometimes, the program finishes with the "Operator new out of memory" when adding elements to the vector.</p>
  
  <p><em>"!Ah, you run out of memory!"</em>- you would say, but that is the strange part. The vector so far has only 275 elements which being short gives 550 bytes. </p>
  
  <p>But this very same program has handled the same vector with many more elements (500 or more) without problem.  </p>
  
  <p><em>Somehow, you previously leaked out memory!</em>- can be said, and I suspect that. Perhaps I used this before and failed to clean it up (although I cleaned it as I stated) but this error appears even when I disconnect and connect the processor wiping out any previous used memory. </p>
  
  <p>I am at lost why this could be happening. Any help or comment or advice greatly appreciated. </p>