title: C: How to free memory to struct when not having a pointer to it anymore?
body: <p>I have a function that makes a structure and returns the new structure itself. The structure contains an array of numbers and an integer showing how many numbers there are(this works like an array).
  So I have makeStruct that makes a temporary struct and then returns it. In main I call this function and assign it to my structure myNumbers.
  This has as an effect that in the function there is created an instance of a structure, copied to myNumbers and then deleted. Is my assumption correct?</p>
  
  <p>This seems to compile correctly but I can't see a way to free the memory allocated as there is no pointer pointing to my structure. How can I free memory?</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  typedef struct{
      int* numbers; /*array of numbers*/
      int  crowd; /*crowd of numbers*/
  } magicNums;
  
  magicNums makeStruct(int crowd){
      magicNums tempStruct, *struct_ptr=&amp;tempStruct; /*my temp struct and its pointer*/
      struct_ptr=(magicNums*)malloc(sizeof(magicNums)); /*allocating memory for struct*/
      tempStruct.numbers=(int*)malloc(sizeof(int)*crowd); /*allocating memory for struct's numbers*/
      return tempStruct;
  }
  
  int main() {
      magicNums myNumbers;
      myNumbers=makeStruct(10);
      /*magicNums *myNumbers_ptr=&amp;myNumbers;  This won't work             */
      /*free(myNumbers_ptr);                  This won't work             */
      /*free(&amp;myNumbers);                     This won't work             */
      return 0;
  }
  </code></pre>