Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Happens If You Set Allocated Memory To NULL in C

Is there a difference between setting a pointer to NULL before and after allocating it?

For example, is there any difference between

char* c = NULL;

and

char* c = malloc(sizeof(char));
c = NULL;

What are the, if any, implications of each of those statements, and is there any difference in calling free(c) in each of those situations?

like image 226
Domn Werner Avatar asked Mar 22 '15 04:03

Domn Werner


4 Answers

Let's look at the two cases.

Case 1: Before malloc

Well, there's almost no harm done here. Unless the pointer was pointing to something before, the pointer is simply set to NULL and points to nothing. Calling free() on a pointer that points to nothing does nothing. "Free the people?" Okay. "Free the free?" You can't really free something that's already sort of "free." No stranger, no danger. Moving on...

Case 2: After malloc

Let's break it down:

char* c = malloc(sizeof(char));
c = NULL;

The first command will reserve memory from the operating system for your program. That's dynamic allocation--getting more memory on the fly.

The second command sets your pointer to NULL. What does that mean? Memory leak. That means your program has basically borrowed memory from the operating system and forgot about it. This is a big no-no. If a program keeps doing it over and over, they will eventually crash (or the entire system will crash).

Just like how you should always return things to friends, you should always return memory to the operating system. Therefore, you must always call free() for every point you malloc(); just like opening and closing brackets (), [], or {} in C or C++.

Never set a pointer to NULL until after you free() the memory associated with it.

like image 73
Cinch Avatar answered Nov 17 '22 22:11

Cinch


Before allocating it, it might have (depending on where it was declared) garbage data in it. By assigning it to null before, you basically assure it is in a known good state.

After allocating and assigning the address to a variable, setting the pointer to null means that the pointer will no longer reference the allocated memory. Normally this creates a memory leak (a bit of allocated memory that cannot be referenced anymore, and so it cannot be freed). However there are times were this is appropriate. For example, if your logic has two references to the memory, one for allocation / deallocation purposes and one for "current item of interest" it might be appropriate to assign the current item of interest and then assign null to it as this "current item of interest" pointer is not related to memory allocation and deallocation.

Finally, if it is a pointer to be used for deallocation, after you free() the pointer, you might want to then set the pointer to null for the same reasons you set the pointer to null prior to allocation (to make sure that your program doesn't get confused by finding an address value in the pointer when that's not memory you want accessed).

like image 20
Edwin Buck Avatar answered Nov 17 '22 22:11

Edwin Buck


Yes there's a difference. The second one causes a memory leak.

As to your second question, if c = NULL, then free(c) will have no effect.

like image 4
emlai Avatar answered Nov 17 '22 22:11

emlai


You're not setting the allocated memory to NULL, you're setting the pointer to NULL so that it no longer points to the memory that you allocated. When you call free on it, you're not freeing the memory that you allocated, since the pointer doesn't point to it.

This is called a memory leak: you've allocated some memory, but you no longer have a pointer to it so you can't free it, so the memory will remain "in use" and unavailable to other programs even though you're not actually using it anymore.

Calling free on a NULL pointer has no effect.

like image 4
Wyzard Avatar answered Nov 17 '22 21:11

Wyzard