Did the lifetime of c_str() changed between g++ 4.8.4 and g++ 5.3.1?
<p>I start processes from a daemon which makes sure that said processes run continuously and get started in the proper order, etc.</p>

<p>At some point, I want to start the process with <code>execv()</code> so I prepare an array of string for the arguments as so:</p>

<pre><code>std::vector&lt;std::string&gt; args;
args.push_back("--debug");
args.push_back("--connect");
args.push_back("10.0.0.5:4040");
...
</code></pre>

<p>In most cases, I have around 10 such arguments.</p>

<p>Now, <code>execv()</code> only access an array of bare pointers. So I do the following to create an array of such pointers:</p>

<pre><code>std::vector&lt;char *&gt; args_p; // sorry, my code actually uses std::vector&lt;char const *&gt; args_p -- so constness is fine here
for(auto a : args)
{
    args_p.push_back(a.c_str());
}
args_p.push_back(nullptr); // list needs to be null terminated
</code></pre>

<p>Then I can call <code>execv()</code> with that last array:</p>

<pre><code>execv(
    args_p[0],
    const_cast&lt;char * const *&gt;(&amp;args_p[0])
);
</code></pre>

<p>That was working perfectly in Ubuntu 14.04 with g++ 4.8.4, but somehow, the <code>c_str()</code> pointers get invalidated when I try to run the same code compiled with g++ 5.3.1.</p>

<p>From my understanding, this should not be since I do not modify the strings between the first loop which creates the array of bare pointers and the <code>execv()</code> call.</p>

<p>The <a href="http://en.cppreference.com/w/cpp/string/basic_string/c_str" rel="nofollow">reference</a> says:</p>

<blockquote>
  <p>The pointer obtained from c_str() may be invalidated by:</p>
  
  <ul>
  <li>Passing a non-const reference to the string to any standard library function, or</li>
  <li>Calling non-const member functions on the string, excluding operator[], at(), front(), back(), begin(), rbegin(), end() and rend().</li>
  </ul>
</blockquote>

<p><strong>P.S.</strong> I already have a fix, I now do an <code>stdup()</code> of the <code>c_str()</code> and it works just fine that way. Only I was hoping to avoid one extra copy of the string if possible...</p>