LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 06-13-2007, 04:33 AM   #1
steven_yu
LQ Newbie
 
Registered: Jun 2007
Posts: 19

Rep: Reputation: 0
how does function memcpy work? Any convertion occured?


See following small program
Code:
#include  <stdio.h>
#include  <unistd.h>


int  main()
{
	unsigned  int  a=0x12345678;
	int  i;
		
	memset(str,0,sizeof(str));    	
	memcpy(str,&a,sizeof(a));	
	printf("a = Ox");
	for(i=0;i<4;++i)
  		printf("%x",str[i]);
	printf("\n");

	return   0;
}
when I run it, the result is 0x78563412, it is reserved of orginal variable a, it is suprised to me and I really want to know why does it happen?

If I use the following method, the result is expected.
Code:
#include  <stdio.h>
#include  <unistd.h>

int  main()
{
	unsigned  int  a=0x12345678;
	unsigned  char   str[4];
	int 	i;

	memset(str,0,sizeof(str));

	for(i=3;i>=0;--i)
		str[3-i] = (a >> (8 * i) ) & 0xff;
	
	printf("a = Ox");
	for(i=0;i<4;++i)
  		printf("%x",str[i]);
	printf("\n");
        return   0;

}
 
Old 06-13-2007, 04:58 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,134

Rep: Reputation: 218Reputation: 218Reputation: 218
Hi

"memcpy" just copies memory. What you see here, is the problem of little endian versus big endian in the CPU. Your first program's output is "undefined". The C standard doesn't specify how the compiler stores types like "int" in memory (just like float or double).

It's a common mistake - very often programs just make a binary dump when saving to file, since it's fast and easy. But it becomes a problem when you try the program with a different CPU.
 
Old 06-13-2007, 04:59 AM   #3
nautilus
Member
 
Registered: Jun 2007
Location: London, Athens
Distribution: Debian, Ubuntu
Posts: 36

Rep: Reputation: 15
Your first program doesn't even compile... You have not declared str... Please repost...
 
Old 06-14-2007, 01:18 AM   #4
steven_yu
LQ Newbie
 
Registered: Jun 2007
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by nautilus
Your first program doesn't even compile... You have not declared str... Please repost...
Thanks.
Oh, My guard, it's my mistake, I copy file error.
Code:
#include  <stdio.h>
#include  <unistd.h>


int  main()
{
	unsigned  int  a=0x12345678;
	int  i;
		
	memset(str,0,sizeof(str));    	
	memcpy(str,&a,sizeof(a));	
	printf("a = Ox");
	for(i=0;i<4;++i)
  		printf("%x",str[i]);
	printf("\n");

	return   0;
}
 
Old 06-14-2007, 01:28 AM   #5
steven_yu
LQ Newbie
 
Registered: Jun 2007
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Guttorm
Hi

"memcpy" just copies memory. What you see here, is the problem of little endian versus big endian in the CPU. Your first program's output is "undefined". The C standard doesn't specify how the compiler stores types like "int" in memory (just like float or double).

It's a common mistake - very often programs just make a binary dump when saving to file, since it's fast and easy. But it becomes a problem when you try the program with a different CPU.
Thanks very much.

When CPU put data in memory, it will convert it according to its type, such as little-endian or big-endian. My CPU is Intel 386, little-endian, so result is so.

Writing careful code.
 
Old 06-14-2007, 11:04 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,490

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Quote:
Originally Posted by steven_yu
When CPU put data in memory, it will convert it according to its type
Strictly speaking, it doesn't really convert the data.

It just stores multi-byte data in one way or another, just like a telephone and a numeric keypad lay out the digits in different ways.

Conversion would mean some data processing to happen while here the native (i.e. faster) way of storing data for a given CPU is simply used.
 
Old 06-19-2007, 06:25 AM   #7
atulsvasu
Member
 
Registered: Apr 2006
Distribution: Gentoo
Posts: 49

Rep: Reputation: 15
Big Endian and Small Endian conventions.
 
Old 06-19-2007, 08:25 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
have you tried:


man memcpy


always the first place to look.
 
Old 06-25-2007, 12:04 PM   #9
srp
LQ Newbie
 
Registered: Jun 2007
Posts: 1

Rep: Reputation: 0
The code you shown - reads one byte at a time and prints that byte. But if you read 'int' (i.e 4-byte value) from that location, it will print correct value.
See the modified program below:

Code:
#include  <stdio.h>
#include  <unistd.h>


int  main()
{
	unsigned  int  a=0x12345678;
	int  i;
	unsigned  char str[4];
		
	memset(str,0,sizeof(str));    	
	memcpy(str,&a,sizeof(a));	
	printf("a = Ox");
	for(i=0;i<4;++i)
  	    printf("%x",str[i]); /* prints one byte at a time*/
	printf("\n");
        /* This prints correct 4-byte value */  
	printf("str = 0x%x", *(int *)str);

	return   0;
}

Subhash

Last edited by srp; 06-25-2007 at 12:11 PM.
 
Old 06-25-2007, 01:33 PM   #10
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,134

Rep: Reputation: 218Reputation: 218Reputation: 218
man swab

I forgot to add - but it's just confusing

Last edited by Guttorm; 06-25-2007 at 01:45 PM.
 
  


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
memcpy or mmap or other problems snowing Programming 3 12-26-2005 11:49 PM
memcpy or mmap or other problems snowing Programming 0 12-26-2005 09:56 PM
how can i speed up memcpy? Thinking Programming 9 10-14-2005 07:53 AM
Need several RGB convertion functions kornerr Programming 4 09-21-2005 05:07 AM
memcpy problems alaios Programming 4 09-17-2005 07:26 AM


All times are GMT -5. The time now is 05:11 AM.

Main Menu
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