1

To copy an array b[] into the array a[], one can use function memcpy as follows; memcpy(a,b,sizeof(a)).
But memcpy simply copies bytes from one place to another.
My questions are:

1.How memcpy copies elements of array b[] into a[] by copying bytes?
2.Why sizeof(a) is supplied as arguments?

I am new to programming so, be gentle.

5
  • 1
    2) is because you cannot have function arguments of array type in C. Commented Jun 18, 2013 at 11:44
  • 1
    There be dragons. memcpy needs to know the length of the data -- that's not available at runtime by querying the first two parameters. But get the length wrong and you write all over memory and things go south real fast. Commented Jun 18, 2013 at 11:47
  • You can make your own memory allocator, which will allocate some structure like {void *data, size_t len} when allocating space for data. Then you won't need to pass length of data to your own memcpy. Commented Jun 18, 2013 at 11:49
  • @HotLicks; That's why we use sizeof(a)? Commented Jun 18, 2013 at 13:19
  • Yes, but sizeof can be tricky. If, instead of a fixed-size array, a is a pointer (which will behave a lot like the fixed-size array variable in most other contexts), taking sizeof(a) can lead to "surprises". Commented Jun 18, 2013 at 14:33

6 Answers 6

6

sizeof(a) is the total size of the array a. For example, in

int a[3];
printf("%d", sizeof(a));

sizeof a will be 12 on most systems (since int is usually 4 bytes and you have 3 of them).

memcpy doesn't know that a is an array. All it knows is that a points to some memory address, so you have to tell it how many bytes to pass. Most places describe memcpy's signature as:

void *memcpy(void *dst, const void *src, size_t nbytes)
Sign up to request clarification or add additional context in comments.

3 Comments

By copying bytes from one place to another,how can it copy data?
@haccks Data is an interpretation of a set of bytes. You read the actual ones and zeros, and interpret them to give you a value. For example, a double is an IEEE754 double, and the bits are laid out in a specific pattern (see the picture en.wikipedia.org/wiki/Double_precision). If you copy all of the bits from one location to another, you can read the bits in the new location and interpret those as a double. You can also interpret it as an int or as a float (although you'll get different values). If you want to experiment, use a union type
You mean if b[]={3,4} then,memcpy will copy 011 and 100 to the destination??
2
#include <string.h>

void *
memcpy(void *s1, const void *s2, register size_t n)
{
    register char *p1 = s1;
    register const char *p2 = s2;

    if(n) {
        n++;
        while (--n > 0) {
            *p1++ = *p2++;
        }
    }
    return s1;
}

Here is a source code for memcpy. As you can see it literally moves through each element from register 1 and moves it into register 2 one by one.

size of is used so it knows how big the register is so basically how many elements it must skip through before it has copied the whole register.

2 Comments

What might be the purpose of the if(n) {?
To check there's actually something there in the register, no point skipping through an empty register. As it will end up being out of bounds.
1

memcpy works just like how assembly instruction MOV works (obviously, memcpy must be implemented using MOV). Difference is just that, we move a block of data, instead of one variable. Size of the block is required since the process need to know how much bytes is to be copied from one location to another.

Comments

0

1) In C, all arrays are really just bytes of data in a long sequence, one after another. There is no interspersed meta data or anything like that, so copying the bytes of an array, means copying the array itself with no loss of data, regardless of what type the array is made up of. Only the total size matters.

2) This is as Kerrek SB said, "because you cannot have function arguments of array type in C". That ties in with 1 - since there is no meta data in C arrays, the function gets no information on how large an array is, just where in memory it begins. The size is so it can know when to stop write data.

3 Comments

If array b[] = {3,4} is copied to array a[2].memcpy copy 8 bytes of data to a[].8 bytes of data also meas 5 and 6.I want to know how the elements of a[] becomes 3 and 4 not any other data of 8 bytes?
3 and 4 have the same representation regardless of where in memory? @haccks. I don't think I understand your question...'
If b[]={3,4} then,will memcpy copy 011 and 100 to the destination?
0

Sizeof(a) is supplied as third arguments because it is the size in bytes of the array a. In fact sizeof called with an array as argument returns the total number of bytes in that array instead of the size of the pointer represented by the array identifier.

Comments

0

First one is an interesting question. Yes, it is a simple binary copy, but an order of copying matters. Say, if you have an overlaping areas, and &a[0] > &b[0], what would happen? If it is from left to right, memcpy would simply overwrite data from b[] while copying.

AFAIK, the particular order of copying isn't specified by any standart and it is recomended to use memmove instead as a safer alternative, but still.

1 Comment

In fact, the memcpy() function is explicitly documented as not to be used if the region to be copied overlaps with the target area. Since you're never going to do that, order of copying doesn't matter, and you can do it whichever way is most efficient. The memmove() function is provided for the copying of (potentially) overlapping regions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.