7

It may be silly question but still am not getting it. I do have a char array say char arr[100] having some data

 char arry[100] ---- some data;
 int test;
 memcpy(&test,array+4,sizeof(int))

What does this memcpy will do Thanks SKP

user3160866
  • 367
  • 2
  • 9

5 Answers5

5

This might be useful in so-called serialization of data.

Say, if someone saved an integer into a file.

Then you read the file into a buffer (arry in your case) as a stream of bytes. Now you want to convert these bytes into real data, e.g. in your case integer test which has been stored with offset 4.

There are several ways to do that. One is to use memcpy to copy bytes into area where compiler would treat them as an integer.

So to answer your question:

 memcpy(&test,array+4,sizeof(int))

...will copy sizeof(int) number of bytes, starting from 4-rth byte from array into memory allocated for variable test (which has type int). Now test has the integer value which was saved into arry originally, probably using the following code:

 memcpy(array+4, &original_int, sizeof(int))

Doing this requires some knowledge of hardware and the language. As there are many complications, among which:

fukanchik
  • 2,811
  • 24
  • 29
2

It just copy the element array[4] to variable test. On 32-bit machine sizeof(int) = 4. memcpy will copy 4 bytes to the address &test which can hold 4 bytes.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    Removed after your edit. The first sentence by itself is wrong, since it implied only one byte is copied when in fact elements 4-7 are copied as you later clarify. – Lee Daniel Crocker Apr 09 '15 at 16:28
2

This will copy probably 4 bytes (depending on your machine and compiler--your int might be bigger or smaller) from the 4th through 7th bytes of arry into the integer test.

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
1

According to the documentation of memcpy() :

void * memcpy ( void * destination, const void * source, size_t num );

Copies the values of num bytes from the location pointed by source directly to the memory block pointed by destination.

In your case :

  • num=sizeof(int)
  • destination=&test A pointer to test
  • source=&array[4] A pointer to the fourth element of the array of char array

Hence, if sizeof(int)==4 it will copy array[4], array[5],array[6] and array[7] to test

There are questions that can help you understand the memory layout of integers :

There is also an issue with endianless : on my computer, array[4] corresponds to the least significant byte.

Consequently, if array[7]=0x80 and array4]=array[5]=array[6]=0x00 then test will contain 00000080 and test will worth -2^31.

if array[7]=0x2A and array[5]=array[6]=array[4]=0x00 then test will contain 2A000000 and test will worth 42 (that is 0x0000002A).

Here is a test code to be compiled by gcc main.c -o main

#include <stdio.h>
#include <string.h>
int main(int  argc,char *argv[]){

    char array[100];
    int test;
    printf("sizeof(int) is %ld\n",sizeof(int));

    array[4]=0x00;
    array[5]=0;
    array[6]=0;
    array[7]=0x80;
    memcpy(&test,&array[4],sizeof(int));
    printf("test worth %d or(hexa) %x\n",test,test);

    array[4]=0x2A;
    array[5]=0;
    array[6]=0;
    array[7]=0x00;
    memcpy(&test,&array[4],sizeof(int));
    printf("test worth %d or(hexa) %x\n",test,test);
    return 0;
}
Community
  • 1
  • 1
francis
  • 9,525
  • 2
  • 25
  • 41
1

Generally the C library function void *memcpy(void *str1,const void *str2,size_t n) copies n characters from memory area str2 to memory area str1, where:

str1 – this is pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*

str2 -- this is pointer to source of data to be copied, type-casted to a pointer of type void*

n -- this is the number of bytes to be copied

memcpy returns a pointer to destination, which is str1

In your case, is copied the contents of the array, from the address pointed to by array[4] up to sizeof (int) bytes (4 bytes in this case, if you have a 32bit machine), the address pointed to by test

Balboa
  • 59
  • 6