LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Getting unpredictible output by memcpy. (http://www.linuxquestions.org/questions/programming-9/getting-unpredictible-output-by-memcpy-4175440942/)

manoj7410 12-11-2012 05:08 AM

Getting unpredictible output by memcpy.
 
hi..
I was trying to copy one address's data to another location by using memcpy, but getting unpredictible output.. code is :

#include<stdio.h>
int main()
{
int *i,j,k = 10;
i = &k;
// *(&j) = *i;
memcpy (&j,i,10);
printf("i = %d, j = %d\n",*i,j);
return 0;
}

value of *i is giving "segmentation fault" and sometimes a positive value or 0.
Don't know how. Please put some light on it.

Thanks

millgates 12-11-2012 05:48 AM

What is the program supposed to do?

What does following statement do?
Code:

memcpy (&j,i,10);

manoj7410 12-11-2012 06:06 AM

Quote:

Originally Posted by millgates (Post 4846883)
What is the program supposed to do?

What does following statement do?
Code:

memcpy (&j,i,10);

"i" is an integer pointer and m trying to copy the data available at i to the address of j.
j is working fine, but *i is giving seg fault or 0 or some negative value.

millgates 12-11-2012 06:10 AM

Code:

memcpy (&j,i,10);
OK, but why 10?

manoj7410 12-11-2012 06:14 AM

Quote:

Originally Posted by millgates (Post 4846901)
Code:

memcpy (&j,i,10);
OK, but why 10?

just 10 bytes, because I dont know the concept behind it.

millgates 12-11-2012 06:19 AM

But you're trying to copy 10 bytes to an int which size might vary depending on your platform but I would bet my Slackware install disc that it is less than 10 bytes. How is it supposed to fit there?

linosaurusroot 12-11-2012 06:19 AM

Your integers are probably 4 bytes, not 10.

Code:

memcpy (&j, i, sizeof(int));

manoj7410 12-11-2012 06:24 AM

Quote:

Originally Posted by linosaurusroot (Post 4846911)
Your integers are probably 4 bytes, not 10.

Code:

memcpy (&j, i, sizeof(int));

4 bytes I tried and was working fine, but if I give 10 bytes then, why i is being disturbed..?

millgates 12-11-2012 06:31 AM

Quote:

Originally Posted by manoj7410 (Post 4846913)
4 bytes I tried and was working fine, but if I give 10 bytes then, why i is being disturbed..?

because i points to k, and (and I think this is unspecified, though) probably k is stored in memory right after j. So, when you memcpy 10 bytes to j, the first 4 bytes (assuming your int is 4 bytes, which it most likely is) will be written to j, the next 4 bytes will rewrite k (which is where i points to, and the last two bytes will try to overwrite whatever happens to be there in the memory, perhaps some instructions or something like that. Of course the system does not like that.

linosaurusroot 12-11-2012 06:48 AM

A guide such as "Smashing the stack for fun and profit" explains what happens when you overflow the bounds of your automatic variables and damage other data on the stack. When programming in C you should take great care not to do this. In fact that's one of the major drawbacks of C.

johnsfine 12-11-2012 07:24 AM

Quote:

Originally Posted by manoj7410 (Post 4846913)
but if I give 10 bytes then, why i is being disturbed..?

Code:

int main()
{
        int *i,j,k = 10;

Assuming optimization is off, i, j and k are stored on the stack, almost certainly together and in a sequence that is up to the whim of the compiler and is not predictable.

Code:

memcpy (&j,i,10);
You overwrite the 4 bytes of j and you overwrite whatever six bytes follow j. So if i happens to be directly after j, you overwrite all four bytes of i (and two more beyond). If k is after j and i after k, then you overwrite all four bytes of k and two bytes of i.

If you overwrite i with something that doesn't happen to be a valid address, then the next use of *i will seg fault.

manoj7410 12-11-2012 09:24 PM

I got it now, Thanks to all..


All times are GMT -5. The time now is 12:27 PM.