Take the 2-minute tour ×

Could somebody tell me the difference between:

int *p;


int *p;
share|improve this question
C is not Java. :) –  vladr Mar 12 '10 at 6:03

7 Answers 7

up vote 28 down vote accepted

free will deallocate the memory that p points to - simply assigning it to NULL will not (and thus you will have a memory leak).

It is worth mentioning that it is good practice to assign your pointer to NULL AFTER the call to free, as this will prevent you from accidentally trying to access the freed memory (which is still possible, but absolutely should not be done).

share|improve this answer
in C, a pointer is literally just a value, like an int. when you set it to a values with '=' literally all that happens is that it gets set to be that value, there is nothing else that happens behind the scenes. –  matt Mar 12 '10 at 7:21

There is no garbage collection in C, so if you don't explicitly free a chunk of memory, it will never get freed, even if there are no references to it. Maybe you come from a background in garbage collected languages, so it might be hard to change your way of thought, but it's always important to remember to "manually" free all resources in low-level languages like C.

Hope this helps Cheers

share|improve this answer
Unless you install a garbage collector, of course ;v) –  Potatoswatter Mar 12 '10 at 7:25

p is pointer (to a block allocated dynamically in memory ["on the Heap"])
This means that p is a variable which contains the address in memory of a particular block (or some particular size, in the example a block big enough to hold 10 integers).

free(p); instructs the memory management logic (of the C Runtime) that the memory previously occupied by the block which p points to can be reused.

p = NULL; sets the value of p to NULL (the address it formerly contained is lost) but the block in memory it pointed too is still considered in use.

There can be some confusion because in languages such as Java, C#, Python etc. merely assigning the variable to NULL (or to another address for that matter), will automatically free the underlying memory (assuming no other references to this address exist in other live variables).

This is not the case in C or C++, leading to errors like the following:

  // possibly some some code etc.
  // later:
  p[6] = 'a';  // <<---  Ouch we're modifying whatever new variable is there !!!


  // didn't call free(p)
  p = NULL;
  // now the memory allocated for p is held in memory even though it
  // is not going to be used (assuming no copies of p or of pointers to part
  // of that block were made.

The latter case is only wasteful of resources, the former can lead to hard to find bugs.

That's why a typical C idiom is:

   p = NULL;
share|improve this answer

Not calling free and directly assigning to NULL will lead to a memory leak as explained by others. You can refer to this link to get clear details about memory leaks and other memory related problems.

share|improve this answer

Freeing the allocated memory deallocates it and allows that memory to be used elsewhere while the pointer to where the memory was allocated is preserved.

Setting a pointer to the allocated memory to NULL does not deallocate it.

If you are using an implementation that uses a heap-based malloc, debug something that allocates, uses and frees memory, and do the same to something that allocates, uses, and sets the pointer to the memory to NULL.

Memory management is implementation-dependent (see http://en.wikipedia.org/wiki/Malloc#Implementations).

share|improve this answer


int *p; p= (int * ) malloc(10*sizeof(int)); free(p);

the memory is released back to the heap. but the pointer still pointing to the freed memory location. Thus if its further used , would lead to a memory corruption.

thus correct thing to do is resetting the pointer to NULL explicitly to avoid further usage of that pointer .

its not advisable to type cast the return pointer in C


int *p; p=(int * )malloc(10*sizeof(int)); p=NULL;

This would result in a memory leak : as the allocated memory is not freed here. you are just reset the pointer to NULL.

share|improve this answer

The better way is to first free the memory and then set it to NULL:

p = NULL;
share|improve this answer

Your Answer


