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 08-22-2007, 09:28 AM   #1
George2
Member
 
Registered: Oct 2003
Posts: 354

Rep: Reputation: 30
segmentation fault in memcpy


Hello everyone,


I am wondering if I met with segmentation fault in memcpy (Red Hat Linux). How to debug it? And what is the possible reason of segmentation fault -- source address error, destination address error or size error?

Sorry that my application is too large and I can not post all the codes.


thanks in advance,
George
 
Old 08-22-2007, 10:38 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Code:
gcc -g -o myapp myapp
gdb myapp
gdb >r <put parameters here>
............
............ core
ba
This will show you the stack frame and what line of code bombed.


Once you know the line add printf statements just before the faulting line, which show all of the values for the arguments to memcpy(). If printf() bombs, then it means the address for the destination or source are invalid, an you can then sprinkle printf's around finding where the pointer problem originated.
 
Old 08-23-2007, 02:20 AM   #3
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Thanks Jim,


Quote:
Originally Posted by jim mcnamara View Post
Code:
gcc -g -o myapp myapp
gdb myapp
gdb >r <put parameters here>
............
............ core
ba
This will show you the stack frame and what line of code bombed.


Once you know the line add printf statements just before the faulting line, which show all of the values for the arguments to memcpy(). If printf() bombs, then it means the address for the destination or source are invalid, an you can then sprinkle printf's around finding where the pointer problem originated.
I have used gdb to debug it. The source and the destination address of buffer seems correct, not NULL. Maybe they point to invalid address as you mentioned. The size is correct, 4096 bytes.

What do you mean printf() bombs? You mean printf() cause segmentation fault?

Could you recommend some approach or some tools to debug? The buffers (both source and destination) are on heap.


regards,
George
 
Old 08-23-2007, 03:49 AM   #4
nhydra
Member
 
Registered: May 2006
Distribution: Fedora Core, FreeSpire, PC-BSD, NetBSD
Posts: 96

Rep: Reputation: 15
Can you post only the source code around this error? Just 10 lines around the line where you got the segfault.
 
Old 08-24-2007, 02:02 AM   #5
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Thanks nhydra,


Quote:
Originally Posted by nhydra View Post
Can you post only the source code around this error? Just 10 lines around the line where you got the segfault.
The code is simple, just accepts source buffer, destination buffer and size as input parameter in a function, then at the beginning of the function, I will do memcpy, which causes segmentation fault.

Any ideas or comments? Any tools to use to detect errors? Any more information needed to analyze this issue?


regards,
George
 
Old 08-24-2007, 02:49 AM   #6
nhydra
Member
 
Registered: May 2006
Distribution: Fedora Core, FreeSpire, PC-BSD, NetBSD
Posts: 96

Rep: Reputation: 15
What is the type of source and dest buffers? Are you sure you have the right pointers in the memory? Wrong address of the pointer can do segfault too.
Another thought... try to copy some data in the destination buffer.
This is the info about the memcpy() function

Code:
void * memcpy (void *restrict TO, const void *restrict
          FROM, size_t SIZE)
 
Old 08-26-2007, 12:33 AM   #7
JoeyAdams
Member
 
Registered: Jun 2006
Distribution: Kubuntu Hardy
Posts: 94

Rep: Reputation: 15
Quote:
Once you know the line add printf statements just before the faulting line
These lines might not show up, so do fflush(stdout); after the printf. The stream might not flush out the text you just printed if you don't, so you won't get to see exactly when the program crashed.
 
  


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
Segmentation fault cybil001 Programming 39 02-19-2007 11:01 AM
Segmentation fault cmplet-noobie Programming 3 04-03-2006 03:52 AM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika Suse/Novell 2 12-02-2005 10:34 AM
Need help: Seg fault, Memcpy, and dynamically allocated arrays benobi Programming 3 06-09-2005 11:58 PM
Segmentation Fault rupesh_pulikool Linux - Software 7 01-07-2005 06:55 AM


All times are GMT -5. The time now is 04:52 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration