title: Are two heap allocations more expensive than a call to std::string fill ctor?
body: <p>I want to have a string with a capacity of 131 chars (or bytes). I know two simple ways of achieving that. So which of these two code blocks is faster and more efficient?</p>
  <pre><code>std::string tempMsg( 131, '\0' ); // constructs the string with a 131 byte buffer from the start
  tempMsg.clear( ); // clears those '\0' chars to free space for the actual data
  
  tempMsg += &quot;/* some string literals that are appended */&quot;;
  </code></pre>
  <p>or this one:</p>
  <pre><code>std::string tempMsg; // default constructs the string with a 16 byte buffer
  tempMsg.reserve( 131 ); // reallocates the string to increase the buffer size to 131 bytes??
  
  tempMsg += &quot;/* some string literals that are appended */&quot;;
  </code></pre>
  <p>I guess the first approach only uses 1 allocation and then sets all those 131 bytes to 0 ('\0') and then clears the string (std::string::clear is <em>generally constant</em> according to: <a href="https://www.cplusplus.com/reference/string/string/clear/" rel="nofollow noreferrer">https://www.cplusplus.com/reference/string/string/clear/</a>).
  The second approach uses 2 allocations but on the other hand, it doesn't have to set anything to '\0'. But I've also heard about compilers allocating 16 bytes on the stack for a string object for optimization purposes. So the 2nd method might use only 1 heap allocation as well.</p>
  <p>So is the first method faster than the other one? Or are there any other better methods?</p>