title: C programming: allocation of pointer lost even when done on the contents of a double pointer
body: <p>So, I have been grappling with this for a while, and after a good deal of late-night hair-pulling I decided to see if you all wanted to take a crack at it.</p>
  
  <p>In summary, I am trying to setup a dynamically allocated struct for use in a program by handing off the allocation code to another function outside of main. I know of two ways of doing this: one, have the function return a pointer to a allocated struct like:</p>
  
  <pre><code>typeStruct *someFunction()
  </code></pre>
  
  <p>Or two, pass in a double pointer to the struct, dereference it, and allocate memory on the inner pointer. Like:</p>
  
  <pre><code>void someFunction(typeStruct **ptr)
  </code></pre>
  
  <p>I am attempting to do the latter, as I want to use the return value for a char flag rather than the struct itself.</p>
  
  <p>Unfortunately, it seems that changes made on a deference of the pointer don't make it past the scope of the function. I managed to replicate my problem with a simpler sample program. Here is the code:</p>
  
  <p>test.c:</p>
  
  <pre><code>#include "testheader.h"
  
  int main() {
  
      char success = 0;
      test *t;
      printf("\nBefore setup (in Main), Test: %d", t);
      success = setupTest(&amp;t);
      printf("\nAfter setup (in Main), Test: %d", t);
  
      free(t-&gt;numbers);
      free(t);
  
      return success;
  }
  </code></pre>
  
  <p>testheader.c:</p>
  
  <pre><code>#include "testheader.h"
  
  const int NUM_NUMBERS = 100;
  
  char setupTest(test **ptr) {
      printf("\nBefore deference, Test: %d", *ptr);
      char flag = 0;
      test *t = *ptr;
      printf("\nBefore malloc, Test: %d", t);
      t = malloc(sizeof(test));
      printf("\nAfter malloc, Test: %d", t);
      if(t != NULL) {
          t-&gt;numbers = malloc(sizeof(int) * NUM_NUMBERS);
          if(t-&gt;numbers != NULL) {
              for(int i = 0; i &lt; NUM_NUMBERS; ++i) t-&gt;numbers[i] = i;
          } else flag = 1;
      } else flag = 1;
      printf("\nEnd of setup, Test: %d", t);
      return flag;
  }
  </code></pre>
  
  <p>testheader.h:</p>
  
  <pre><code>#ifndef TESTHEADER_H
  #define TESTHEADER_H
  
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  typedef struct test_t {
  
      int *numbers;
  
  } test;
  
  char setupTest(test **);
  
  #endif
  </code></pre>
  
  <p>Here is how I am compiling it: gcc test.c testheader.c -o test.exe</p>
  
  <p>I am using gcc 5.1.0 to compile in git bash and Windows 10's console to run.</p>
  
  <p>Here is my output from running test.exe:
  <a href="https://i.stack.imgur.com/BMt1j.png" rel="nofollow noreferrer">Console debugging</a></p>
  
  <p>And then it crashes. As one can see, the inside the function behaves as expected, although the code outside it does not reflect the allocation made inside, as if I had just passed the pointer itself rather than passing by reference with a double pointer. </p>
  
  <p>Is there something I am missing here?</p>
  
  <p>Diagnosis:</p>
  
  <p>The lines</p>
  
  <pre><code>test *t = *ptr;
  t = malloc(sizeof(test));
  </code></pre>
  
  <p>Only effect the local variable t, and not the contents of **ptr.</p>
  
  <p>Solution:</p>
  
  <p>malloc on the contents of **ptr rather than a local variable so that the allocation doesn't go out of scope upon return. And swap the two lines.</p>
  
  <pre><code>*ptr = malloc(sizeof(test));
  test *t = *ptr;
  </code></pre>