title: Basic C pointer allocation/deallocation
body: <p>Writing code in C, never formally learned any of it, using GNU's GSL library, quick fundamental question. </p>
  
  <p>Correct me if I'm wrong, but the way I understand it, when I allocate memory to use for my matrices (using the built-in <code>var = gsl_matrix_alloc(x,x)</code>) and store them in a variable, I'm essentially creating 
  a pointer, which is simply some address of memory, like: 
  x01234749162</p>
  
  <p>which POINTS to the first pointer/memory location of my GSL matrix. Tracking when to deallocate the memory of the structure associated with a pointer (again, built-in <code>gsl_matrix_free(x,x,x)</code>) is no problem, and I understand I need to do this before I reassign the pointer of the structure, otherwise I've created a memory leak. </p>
  
  <p>So now to my question, and again, I know this is basic, but hear me out--I couldn't find a particularly straight answer on stackoverflow, mostly because a lot of the answers involve C++ rather than C--how do I free the pointer to the structure itself? </p>
  
  <p>Everyone says "oh, just set it to NULL". Why would that work? That's just changing the memory address that POINTS to the deallocated structure. Does that tell the MMU that that memory location is okay to use now? When I debug my program in XCode, for example, all of the properties of the gsl_matrix structure is successfully deallocated; everything just becomes this garbage string of random hex characters, which is what free'ing memory is supposed to do. But, I can still see the variable name (pointer) while stepping through the debugger... even if I set the variable to NULL. I would interpret that as meaning I did not free the pointer, I just freed the structure and set it to x0000000 (NULL). </p>
  
  <p>Am I doing everything correct, and this is just a feature of XCode, or am I missing something basic? </p>
  
  <p>And I realize that a single pointer to a structure, if the structure is deallocated, could be argued to not be a big deal, but it matters. </p>
  
  <p>Here's some code to try to illustrate my thoughts. </p>
  
  <pre><code>gsl_matrix* my_matrix; 
  // create single memory address in memory, not pointing to anything yet 
  
  my_matrix = gsl_matrix_alloc(5, 5); 
  // allocates 25 memory spaces for the values that the pointer held by my_matrix 
  // points too
  // Note: so, now there's 26 memory spots allocated to the matrix, excluding other
  // properties created along with the my-matrix structure, right? 
  
  gsl_matrix_free(my_matrix); // deallocates those 25 spaces the structure had, 
  // along with other properties that may have been automatically created
  free(my_matrix); // SIGBRT error. Is the pointer to the deallocated structure
  // still using that one memory address?  
  my_matrix = NULL; // this doesn't make sense to me.I get that any future referral
  // to the my_matrix pointer will just return garbage, and so setting a pointer to
  // that can help in debugging, but can the pointer--that is just one memory 
  // address--be completely deallocated such that in the debugger the variable name
  // disappears?  
  </code></pre>