ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
void main()
{
..........
............
int *b = (int*)malloc(10);
free(b);
b = NULL; ////See Here
..............
...........
}
Now, I have a doubt that what is the importance of the last statement (b = NULL).... I think that this is rigorous coding and prevents from accessing memory which should not be accessed by program.
Then I think if it is so what the hell OS and Compiler will do for memory management....
Please tell me to include the last statement in the code is worth good or it is non-useful.
P.S... If you include the last statement and run the code on win platform... It will not run longer if I access "b" later... If I dont include the last statement, it rund quite a longer... Not checked in Linux though...
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
IMHO this is defensive programming. If you have the habit to check a pointer for being non-null before it is used, with setting it to NULL you make it clear no memory is allocated.
If you omit setting it to NULL or you don't check on it being NULL, chances are you use a pointer to non-allocated memory.
If you don't make any errors anywhere, you program runs just fine. However most of us do make errors.
If your program gets larger, and does a lot of freeing and allocating, I think it is a good practice to set a pointer to NULL.
jlinkels is right. The main benefit of setting the variable "b" to (what I assume is) 0x0 after you free it is that later accidental accesses to an unallocated "b" will result in a SIGSEGV. Having your program crash with a consistent, and therefore easier to debug, behaviour is much better than reading / writing other unknown memory in your address space.
Echoing what has been said, leaving dangling pointers is not only a bad thing, its very dangerous as well.
Other programs may later allocate memory from that space while you're executing, and your variable (b) will allow your program to change that memory without proper permission. Sure, most desktop OSes include segmentation and virtual memory to protect us against this, but many embedded OSes are not as defensive.
ALWAYS make sure that you cleanup dangling pointers after you've freed memory. The consequences of not cleaning memory are way too dangerous.
It might be a good idea to have a macro that you can use to call free, and automatically set the pointer to null. It's a good way to ensure that every pointer is returned to 0.
Warning about initializing pointers to 0 religiously: If the next statement is going to assign something else to the pointer, it is easier and clearer (to me) to just initialize it to the something else in the first place. It will not compromise defensive programming. If you are writing a destructor for a struct and that frees the pointers inside, it is fairly safe not to assign those pointers to 0, since the lifetime of the dangling pointer is known to be small.
That said, anything done religiously is bad, so no surprise there.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.