LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   segmentation fault in memcpy (https://www.linuxquestions.org/questions/programming-9/segmentation-fault-in-memcpy-578927/)

George2 08-22-2007 08:28 AM

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

jim mcnamara 08-22-2007 09:38 AM

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.

George2 08-23-2007 01:20 AM

Thanks Jim,


Quote:

Originally Posted by jim mcnamara (Post 2866896)
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

nhydra 08-23-2007 02:49 AM

Can you post only the source code around this error? Just 10 lines around the line where you got the segfault.

George2 08-24-2007 01:02 AM

Thanks nhydra,


Quote:

Originally Posted by nhydra (Post 2867734)
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

nhydra 08-24-2007 01:49 AM

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)


JoeyAdams 08-25-2007 11:33 PM

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.


All times are GMT -5. The time now is 09:29 PM.