LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 02-23-2011, 08:35 AM   #1
manohar
Member
 
Registered: Dec 2010
Posts: 42

Rep: Reputation: 2
Smile Issue with malloc


Hi,
Any one faced segmentation problem. Iam facing this issue. Iam doing dynamic allocation and freeing properly after using that. My project is running for 2-3hrs after that iam getting segmentation problem. From the man pages Iam get to know below information on malloc bugs.

http://linux.die.net/man/3/malloc

By default, Linux follows an optimistic memory allocation strategy. This means that when malloc() returns non-NULL there is no guarantee that the memory really is available. This is a really bad bug. In case it turns out that the system is out of memory, one or more processes will be killed by the infamous OOM killer.

Iam thinking the above scenario is happening for my case. How to know...? Has ny one faced with this issue...!!. Please help me in this.
 
Old 02-23-2011, 08:57 AM   #2
manohar
Member
 
Registered: Dec 2010
Posts: 42

Original Poster
Rep: Reputation: 2
Smile

Added to earlier Q. How free knows how much memory need to free. even after free also ptr won't become NULL...?
 
Old 02-23-2011, 09:20 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by manohar View Post
Iam doing dynamic allocation and freeing properly after using that.
So when we try to help you, we should ignore a large area that might contain your bug because you say you're doing that "properly"?

I expect you think each part of your program is doing its job properly. But some part isn't, or you wouldn't have this bug.

Quote:
My project is running for 2-3hrs after that iam getting segmentation problem.
You should investigate the details of the seg fault with gdb. At minimum get a backtrace to see where in your code the seg fault occurs.

Quote:
From the man pages Iam get to know below information on malloc bugs.
A lot of people do think optimistic memory allocation is a bug. They're wrong.

Quote:
Iam thinking the above scenario is happening for my case. How to know...?
That scenario does not fit the symptoms you described. The bug is in your own code.

If you want to be extra certain that supposed "bug" in malloc cannot affect you, you just need to make sure the swap partition on your Linux system is at least a few hundred MB larger than the largest amount of swap your system ever uses. Disk space is cheap so making swap larger than needed should be an insignificant waste of disk space.

Contrast the symptoms you reported in the current thread with what you described in a different recent thread:
http://www.linuxquestions.org/questi...killed-862964/
There isn't enough info there to be sure, but the effects of optimistic allocation are one possibility that might be considered for the symptoms in that thread. If your system has significant extra swap space, then I wouldn't even consider optimistic allocation as a possibility in that thread.
In this thread, even without knowing how much swap space is free, the symptom is wrong for that issue.

Quote:
Originally Posted by manohar View Post
How free knows how much memory need to free.
malloc() actually allocates slightly more for each chunk than the amount you request, then it uses a small part at the beginning of what it allocated to store some info to be used later by free(). It passes you a pointer not to the beginning of what was allocated, but to just past the extra it allocated for use by free(). So you get a pointer to a chunk of the size you requested and the extra space it allocated is in the memory immediately before your chunk.

In that extra space, it stores info that lets it deduce the size of the chunk to be freed as well as info to find the next and previous chunks, so that if one or both of those is already free when this chunk gets freed, free() can consolidate small chunks into big chunks.

All the above only applies to ordinary size chunks. Any entirely different system is used for super large chunks. But extra memory is allocated somewhere by malloc() to contain info that free() needs in order to do its job.

Quote:
Originally Posted by manohar View Post
even after free also ptr won't become NULL...?
That is correct. If your code expects the ptr to become null when the memory it points to is freed, that is a bug in your code, not a bug in free().

After freeing the memory pointed to by a pointer, your code must not access that memory again. But nothing other than the correctness of your own code actually stops you from accessing that memory. If you do access memory that has already been freed, your code probably won't fail right at the point of that error. If that error causes any symptom at all, it usually is a failure much later in code that seems to be unrelated.

Last edited by johnsfine; 02-23-2011 at 09:46 AM.
 
  


Reply

Tags
[c]



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
[SOLVED] C - How to malloc for a structure golmschenk Programming 5 03-08-2010 12:16 PM
malloc anoosh Programming 1 03-15-2006 04:41 PM
malloc eagle683 Programming 6 05-22-2005 02:40 PM
malloc() vijeesh_ep Programming 4 08-25-2004 03:50 PM
about malloc eshwar_ind Programming 11 02-18-2004 03:41 PM

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

All times are GMT -5. The time now is 06:10 PM.

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