Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 12-10-2003, 03:30 PM   #1
Registered: Dec 2003
Posts: 33

Rep: Reputation: 15
malloc/free and segfault - advanced question


A chunk of memory is malloc()ed.
A pointer p points to a memory location in that chunk.
The memory is then free()ed.

Can deferencing the pointer p cause a segmentation under Linux some times and
other times not.

In otherwords, can accessing memory not malloc()ed (but still in possible valid range) cause segmentation faults some times and not other times.

(Of course I know the code will be buggy, it's because I am having underterministic segmentation faults with the same program sometimes and other times not and I am trying to figure out what goes on. Note: The code is very complex.)

In case it matters, this is on a PowerPC processor.

Thanks in advance,
Old 12-10-2003, 03:38 PM   #2
jim mcnamara
Registered: May 2002
Posts: 964

Rep: Reputation: 36
A segfault means that the code tried to reference memory outside one of the segments. malloc creates heap memory which is normally outside of the linker defined segments. Once the pointer is free() then
other malloc calls can reuse that memory.

The reason you don't always segfault is that the memory has been reused.
It is marked as a valid part of process space. But. There is no way to know precisely what the pointer is doing. When you segfault it simply means that you followed another code path (one that did not happen to make a malloc call that reused memory)

Short answer: yes.
Old 12-10-2003, 04:38 PM   #3
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15

If you have a core file, you could use gdb to determine where the segmentation fault occured.

gdb <exe-name> core

then type 'where' and that will give you a stack trace..

Also try running your program through some type of runtime memory checking utility like valgrind..

Old 12-10-2003, 04:51 PM   #4
jim mcnamara
Registered: May 2002
Posts: 964

Rep: Reputation: 36
He can also try lint


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
ADVANCED QUESTION !! Try This extremebfn Linux - Networking 1 01-22-2005 06:57 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 > Forums > Non-*NIX Forums > Programming

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

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