title: Design a method or function which returns a valid string
body: <p>Based on the idea of this entry <a href="https://stackoverflow.com/questions/2514539/is-it-a-good-idea-to-return-const-char-from-a-function">Is it a good idea to return “ const char * ” from a function?</a></p>
  
  <p>I thought to extend this with another question I have.</p>
  
  <p>Consider the following code:</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;cstdio&gt;
  
  const char * GetSomeString() 
  { 
    std::string somestlstring;
    somestlstring = "Hello World!";
  
    return somestlstring.c_str(); 
  }
  
  int main()
  {
    const char * tmp = GetSomeString();
  
    printf("%s\n", tmp);  
  
    return 0;
  }
  </code></pre>
  
  <p>If I build it with</p>
  
  <pre><code>g++ source.cpp -o executable
  </code></pre>
  
  <p>and execute that, I get strange symbols displayed. This is because somestlstring is destroyed through the callstack and the pointer you keep after returning became invalid.</p>
  
  <p>My question is: how should I design a method or function that does not have such behaviour without actually declaring additional global variables or potential member functions?</p>