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-11-2012, 05:08 AM   #1
manoj7410
Member
 
Registered: Jun 2012
Location: India
Distribution: 2.6.x
Posts: 40

Rep: Reputation: Disabled
Lightbulb 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
 
Old 12-11-2012, 05:48 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
What is the program supposed to do?

What does following statement do?
Code:
memcpy (&j,i,10);
 
Old 12-11-2012, 06:06 AM   #3
manoj7410
Member
 
Registered: Jun 2012
Location: India
Distribution: 2.6.x
Posts: 40

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
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.
 
Old 12-11-2012, 06:10 AM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Code:
memcpy (&j,i,10);
OK, but why 10?
 
Old 12-11-2012, 06:14 AM   #5
manoj7410
Member
 
Registered: Jun 2012
Location: India
Distribution: 2.6.x
Posts: 40

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by millgates View Post
Code:
memcpy (&j,i,10);
OK, but why 10?
just 10 bytes, because I dont know the concept behind it.
 
Old 12-11-2012, 06:19 AM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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?
 
Old 12-11-2012, 06:19 AM   #7
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Your integers are probably 4 bytes, not 10.

Code:
memcpy (&j, i, sizeof(int));
 
Old 12-11-2012, 06:24 AM   #8
manoj7410
Member
 
Registered: Jun 2012
Location: India
Distribution: 2.6.x
Posts: 40

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by linosaurusroot View Post
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..?
 
Old 12-11-2012, 06:31 AM   #9
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by manoj7410 View Post
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.
 
1 members found this post helpful.
Old 12-11-2012, 06:48 AM   #10
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
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.
 
Old 12-11-2012, 07:24 AM   #11
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by manoj7410 View Post
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.

Last edited by johnsfine; 12-11-2012 at 07:26 AM.
 
1 members found this post helpful.
Old 12-11-2012, 09:24 PM   #12
manoj7410
Member
 
Registered: Jun 2012
Location: India
Distribution: 2.6.x
Posts: 40

Original Poster
Rep: Reputation: Disabled
I got it now, Thanks to all..
 
  


Reply

Tags
memory



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
memcpy doubt mr.cracker Linux - Newbie 1 05-11-2012 12:42 PM
memcpy and copy_to/from_user rbhsrh Linux - Kernel 2 02-19-2012 10:02 AM
[SOLVED] segmentation fault during memcpy() Aquarius_Girl Programming 10 02-20-2010 04:41 AM
how can i speed up memcpy? Thinking Programming 9 10-14-2005 07:53 AM
memcpy problems alaios Programming 4 09-17-2005 07:26 AM

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

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