title: when to free pointer in C and how to know if it is freed
body: <p>I am new in C, trying to figure out about memory allocation in C that I kinda confused</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct
  {
      int a;
  } struct1_t;
  
  int main()
  {
      funct1(); //init pointer
      return 1;
  }
  
  
  int funct2(struct1_t *ptr2struct)
  {
      printf("print a is %d\n",ptr2struct-&gt;a);
      //free(ptr2struct);
      printf("value of ptr in funct2 is %p\n", ptr2struct);
      return 1; //success
  }
  
  
  int funct1(){
  
      struct1_t *ptr2struct = NULL;
      ptr2struct = malloc(sizeof(*ptr2struct));
      ptr2struct-&gt;a = 5;
      printf("value of ptr before used is %p", ptr2struct);
      if (funct2(ptr2struct) == 0) {
          goto error;
      }
      free(ptr2struct);
  
      printf("value of ptr in funct1 after freed is is %p\n", ptr2struct);
      return 1;
  
  error:
      if(ptr2struct) free(ptr2struct);
      return 0;
  }
  </code></pre>
  
  <p>I have funct 1 that calls funct 2, and after using the allocated pointer in funct1, I try to free the pointer. And I create a case where if the return value in funct2 is not 1, then try again to free the pointer.</p>
  
  <p>My question is below</p>
  
  <p>which practice is better, if I should free the memory in funct2 (after I pass it) or in funct1 (after I finish getting the return value of funct1)
  The second thing is whether this is correct to make a goto error, and error:</p>
  
  <pre><code>if(ptr2struct) free(ptr2struct); 
  </code></pre>
  
  <p>My third question is , how do I check if the allocated value is already freed or not? because after getting the return value, I free the pointer, but if I print it, it shows the same location with the allocated one (so not a null pointer).</p>