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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-22-2007, 08:28 AM
|
#1
|
|
Member
Registered: Oct 2003
Posts: 354
Rep:
|
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
|
|
|
|
08-22-2007, 09:38 AM
|
#2
|
|
Member
Registered: May 2002
Posts: 964
Rep:
|
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.
|
|
|
|
08-23-2007, 01:20 AM
|
#3
|
|
Member
Registered: Oct 2003
Posts: 354
Original Poster
Rep:
|
Thanks Jim,
Quote:
Originally Posted by jim mcnamara
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
|
|
|
|
08-23-2007, 02:49 AM
|
#4
|
|
Member
Registered: May 2006
Distribution: Fedora Core, FreeSpire, PC-BSD, NetBSD
Posts: 96
Rep:
|
Can you post only the source code around this error? Just 10 lines around the line where you got the segfault.
|
|
|
|
08-24-2007, 01:02 AM
|
#5
|
|
Member
Registered: Oct 2003
Posts: 354
Original Poster
Rep:
|
Thanks nhydra,
Quote:
Originally Posted by nhydra
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
|
|
|
|
08-24-2007, 01:49 AM
|
#6
|
|
Member
Registered: May 2006
Distribution: Fedora Core, FreeSpire, PC-BSD, NetBSD
Posts: 96
Rep:
|
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)
|
|
|
|
08-25-2007, 11:33 PM
|
#7
|
|
Member
Registered: Jun 2006
Distribution: Kubuntu Hardy
Posts: 94
Rep:
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:38 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|