Question

Could somebody tell me the difference between:

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

or

int *p;
p=(int*)malloc(10*sizeof(int));
p=NULL;
Was it helpful?

Solution

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).

OTHER TIPS

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

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:

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

or

  // 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:

   free(p);
   p = NULL;

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.

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

free(p);
p = NULL;

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).

1.

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

2.

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.

p=NULL does not deallocate the memory. If you don't need to use the memory pointed by p anymore you should use free() otherwise there will be memory leak. You should also set p=NULL after calling free() to avoid erroneously accessing that memory again in the future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top