title: Returning const char*; how ugly is static?
body: <p>For reasons beyond my control, I need to return <code>const char*</code> from a function, but I don't know what the <code>char</code>s need to be at compile time.  My solution is something like the following:</p>
  
  <pre><code>const char* __str__() {
    static std::string String;
    String = [some fancy stuff];
    return String.c_str();
  }
  </code></pre>
  
  <p>The <code>static</code> prevents the string's destruction on exiting the function, but it also means that the memory sticks around until my program exits (right?).  Because the returned string can occasionally be <em>huge</em> (GBs), this can be a real problem.</p>
  
  <p>I usually avoid pointers at all costs and only ever use <code>static</code> for class members, so I'm not 100% sure what I'm doing.  Is this guaranteed to work?  Is there a better way?</p>
  
  <p>[The context of this question is printing a complicated object in python, using the <code>__str__</code> method.  I define the method in my c++ code, which is then wrapped by SWIG.  The SWIG example shows the use of <code>static</code>, but it's not clear to me that that's the only way.  I am open to suggestions.]</p>