title: How can I write this C function so the users can call it without using "free" afterwards?
body: <p>I am very new to C programming and have a basic question about memory management.</p>
  <p>I have written the following code (which works!) to encode some ASCII characters into a certain format I need:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;stdlib.h&gt;
  
  const char *hexdigits = &quot;0123456789ABCDEF&quot;;
  
  /*
   * Each ASCII byte is encoded as two hexadecimal digits.
   */
  char* makeHex (uint8_t* inputBuffer, int inputSize) {
      int i, j;
      char* compositionBuffer = (char*) malloc(inputSize*2 + 1);
      for (i = j = 0; i &lt; inputSize; i++) {
          unsigned char c;
          c = (inputBuffer[i] &gt;&gt; 4) &amp; 0xf;
          compositionBuffer[j++] = hexdigits[c];
          c = inputBuffer[i] &amp; 0xf;
          compositionBuffer[j++] = hexdigits[c];
      }
      return compositionBuffer;
  }
  
  int main(void) {
    char a[] = &quot;ABCD&quot;;
    uint8_t* inputBuffer = (uint8_t*) a;
    int inputSize = sizeof(inputBuffer);
    int q;
    for (q = 0; q &lt; 10000; q++) {
      char* p = makeHex(inputBuffer, inputSize);
      puts(p); // This outputs &quot;41424344&quot; which is correct.
      free(p); // I am interested in this line
    }
  }
  </code></pre>
  <p>Now, I'd like to be able to perform this function <em>without</em> having to remember to run <code>free(p)</code> every time. If I simply remove that line, there is a memory leak.</p>
  <p>Please can somebody suggest an alternative way to write this, so the users of the function don't need to concern themselves with memory management?</p>