title: Memory allocation for std::string of various lengths
body: <pre><code>#include &lt;iostream&gt;
  
  #include &lt;string&gt;
  
  static uint32_t s_AllocCount = 0;
  
  // Overload of new operator.
  void* operator new(size_t size) {
  
      s_AllocCount++;
      std::cout &lt;&lt; "Allocating " &lt;&lt; size &lt;&lt; " bytes\n";
  
      return malloc(size);
  }
  
  void PrintName(const std::string&amp; name) {
  
      std::cout &lt;&lt; name &lt;&lt; " - " &lt;&lt; name.length() &lt;&lt; " characters." &lt;&lt; std::endl;
  }
  
  int main() {
  
      std::string name = "123456789012345";
      PrintName(name);
  
      std::cout &lt;&lt; s_AllocCount &lt;&lt; " allocations" &lt;&lt; std::endl;
      std::cin.get();
  }
  </code></pre>
  
  <p>The above code is a slightly modified version of that in <a href="https://www.youtube.com/watch?v=ZO68JEgoPeg" rel="nofollow noreferrer"><em>How to make your STRINGS FASTER in C++!</em></a>.</p>
  
  <p>I am puzzled about the various outputs from this program in Visual Studio 2019 compiled in x86 debug.</p>
  
  <p>When <code>std::string name = "123456789012345";</code> the following results:</p>
  
  <p><a href="https://i.stack.imgur.com/HL9rk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HL9rk.png" alt="enter image description here"></a></p>
  
  <p>I understand that 8 bytes are being allocated for the pointer address.  What I <strong>don't</strong> understand is that there are 15 bytes in this string.  Does not the pointer address point to an 8 byte block of memory, i.e., one which can hold 8 characters?</p>
  
  <p>When <code>std::string name = "1234567890123456";</code>, i.e., lengthened by 1 character to 16, the result is:</p>
  
  <p><a href="https://i.stack.imgur.com/Trq4b.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Trq4b.png" alt="enter image description here"></a></p>
  
  <p>Now, an additional 32 bytes of memory have been allocated, clearly enough to hold 16 characters.</p>
  
  <p>Do I understand correctly:</p>
  
  <ol>
  <li><p>That in the <strong>first</strong> case <strong>only</strong> 8 bytes of heap memory are being allocated for storing the string's pointer address and that the 15 characters of the string are stored on the stack?</p></li>
  <li><p>In the <strong>second</strong> case that 40 bytes of heap memory are being allocated and that <strong>both</strong> the string's pointer address and the string characters are all stored on the heap?</p></li>
  </ol>
  
  <p>This question was suggested by the author of the referenced video regarding the use of <code>std::string</code> in OpenGL.  Specifically, he suggested that the willy-nilly use <code>std::string</code> in OpenGL can substantially slow dynamic scene changes.  Now I am trying to better understand just how much memory is being allocated and where so that at least I know when it might be slowing a program.</p>
  
  <p>Since I slightly modified this program, perhaps the code is not doing what I think it is doing, i.e., that I have made a programming mistake.  If that is the case, I would appreciate the error(s) being identified.</p>