LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   how does function memcpy work? Any convertion occured? (http://www.linuxquestions.org/questions/programming-9/how-does-function-memcpy-work-any-convertion-occured-561425/)

steven_yu 06-13-2007 04:33 AM

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;

}


Guttorm 06-13-2007 04:58 AM

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.

nautilus 06-13-2007 04:59 AM

Your first program doesn't even compile... You have not declared str... Please repost...

steven_yu 06-14-2007 01:18 AM

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;
}


steven_yu 06-14-2007 01:28 AM

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.

jlliagre 06-14-2007 11:04 AM

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.

atulsvasu 06-19-2007 06:25 AM

Big Endian and Small Endian conventions.

bigearsbilly 06-19-2007 08:25 AM

have you tried:


man memcpy


always the first place to look.

srp 06-25-2007 12:04 PM

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

Guttorm 06-25-2007 01:33 PM

man swab

I forgot to add - but it's just confusing :)


All times are GMT -5. The time now is 02:22 PM.