Function with return value char* returning garbage
<p>I'm trying to build a utility class that I can re-use, that converts a <code>std::string to a char*</code>:</p>

<pre><code>char* Foo::stringConvert(std::string str){
      std::string newstr = str;
      // Convert std::string to char*
      boost::scoped_array&lt;char&gt; writable(new char[newstr.size() + 1]);
      std::copy(newstr.begin(), newstr.end(), writable.get());
      writable[newstr.size()] = '\0'; 
      // Get the char* from the modified std::string
      return writable.get();
}
</code></pre>

<p>The code works when I tried to load the output from within the stringConvert function, however when used in other parts of my application, this function returns garbage.</p>

<p>For example:</p>

<pre><code>Foo foo;
char* bar = foo.stringConvert(str);
</code></pre>

<p>The above code returns garbage.
Is there any workaround for this kind of issue?</p>
