LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-07-2006, 12:53 AM   #1
vkmgeek
Member
 
Registered: Feb 2006
Location: Ahmedabad
Distribution: rhel5
Posts: 185
Blog Entries: 2

Rep: Reputation: 31
Malloc, Free and OS role


Hi

#include <stdio.h>

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...
 
Old 11-07-2006, 05:20 AM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 11-07-2006, 05:59 AM   #3
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
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.
 
Old 11-07-2006, 09:18 AM   #4
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
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.
 
Old 11-07-2006, 10:36 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
I agree: initializing pointers to NULL when you declare them, and then setting them to NULL after you free them is excellent programming practice!

The sad irony is that it's usually those programmers who DON'T follow this practice ... who would really benefit the most ;-)
 
Old 11-07-2006, 11:46 PM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
*** glibc detected *** malloc() / free()/ double RohanShrivastav Programming 12 10-01-2012 10:08 AM
LXer: The Role of Binary Drivers in a Free OS LXer Syndicated Linux News 0 08-05-2006 07:21 AM
how does malloc() and free() functions work? kuna Programming 8 12-04-2004 03:59 AM
malloc/free in C h/w Programming 12 02-26-2004 01:13 PM
Is my malloc/free thinking correct? registering Programming 6 06-18-2003 11:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration