LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-10-2003, 03:30 PM   #1
iTux
Member
 
Registered: Dec 2003
Posts: 33

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


Hi,

If
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,
iTux
 
Old 12-10-2003, 03:38 PM   #2
jim mcnamara
Member
 
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
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
iTux,

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..

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

Rep: Reputation: 36
He can also try lint
 
  


Reply



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

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

All times are GMT -5. The time now is 12:07 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