title: Dynamically allocating memory for const char string using malloc()
body: <p>I am writing a program that reads a value from an .ini file, then passes the value into a function that accepts a PCSTR (i.e. const char *). The function is <code>getaddrinfo()</code>.</p>
  
  <p>So, I want to write <code>PCSTR ReadFromIni()</code>. To return a constant string, I plan on allocating memory using <code>malloc()</code> and casting the memory to a constant string. I will be able to get the exact number of characters that were read from the .ini file.</p>
  
  <p><strong>Is that technique okay?</strong> I don't really know what else to do.</p>
  
  <p>The following example runs fine in Visual Studio 2013, and prints out "hello" as desired. </p>
  
  <pre><code>const char * m()
  {
      char * c = (char *)malloc(6 * sizeof(char));
      c = "hello";
      return (const char *)c;
  }    
  
  int main(int argc, char * argv[])
  {
      const char * d = m();
      std::cout &lt;&lt; d; // use PCSTR
  }
  </code></pre>