Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

memcpy a buffer and an array not working

Tags:

arrays

c

memcpy

I have a requirement in which i need to pass an empty array as a parameter to a function. And in this called function, i should be memcpy some data into the passed array. So i have written a small example which is same as my requirement. Below is its code:

#include <stdio.h>
#include <stdlib.h>
void printArr(int *a)
{
    int i;
    int *b=(int*)malloc(sizeof(int)*10);
    printf("\n\nEnter 10 lements:\n");
    for(i=0;i<10;i++)
        scanf("%d",&b[i]);

    printf("\nContents of array b:\n");
    for(i=0;i<10;i++)
        printf("%d\t",b[i]);
    printf("\n");
    memcpy(a,b,10);
    printf("\nContents of array a:\n");
    for(i=0;i<10;i++)
        printf("%d\t",a[i]);
    printf("\n");
}
int main()
{
    int a[10];
    printArr(a);
    return 0;
}

In the above example, i'm sending an array from the main function to the printArr function. Now in the called function, data will be memcpy into the array. When the array contents are printed i get some junk values. Also the compilation gives a warning as shown below:

$ gcc -o arr array.c
array.c: In function ‘printArr’:
array.c:15:2: warning: incompatible implicit declaration of built-in function ‘memcpy’

The output of the above program is as shown below:

Enter 10 lements:
0 1 2 3 4 5 6 7 8 9

Contents of array b:
0   1   2   3   4   5   6   7   8   9   

Contents of array a:
0   1   134479874   11136160    11136160    11132916    134514160   134513696   134514171   11132916    

Can someone please tell me what's wrong in the above program.

Note: I need to copy data from the buffer to passed array only using memcpy and not via a for loop because of performance reasons in my actual program.

Thanks in advance.

like image 941
Zax Avatar asked Apr 03 '13 06:04

Zax


People also ask

Does memcpy work with arrays?

Memcpy copies data bytes by byte from the source array to the destination array. This copying of data is threadsafe. The process of copying data can fail if the given size is not accurate for the destination array.

What can I use instead of memcpy?

memmove() is similar to memcpy() as it also copies data from a source to destination.

Does memcpy overwrite or append?

memcpy replaces memory, it does not append.

Does memcpy check overflow?

memcpy() function in C/C++ h” header file in C language. It does not check overflow.


2 Answers

memcpy(a,b,10);

The third argument it the number of bytes to copy. You want memcpy(a, b, 10 * sizeof *a).


Also, you are missing an #include <string.h>, that's why you get the warning.

like image 189
cnicutar Avatar answered Sep 23 '22 12:09

cnicutar


The memcpy() function usage is the following:

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

where num is a number of bytes.

In order to fix this you need to use it the following way:

memcpy(a,b,10*sizeof(int));

Because usually size of an integer is 4 bytes (depending on the platform, compiler, etc).

In your program you copy only 10 bytes instead of 40 bytes. So, in this case you are getting the first "2,5" elements initialized in the array a[] and the rest contains garbage.

EDIT: Also you forgot to #include <string.h>, so this causes the following compilation warning:

array.c:15:2: warning: incompatible implicit declaration of built-in function ‘memcpy’

For the future, please give an attention to the compiler warnings, as it will allow to you to avoid a lot of run-time errors.

like image 29
Alex Avatar answered Sep 25 '22 12:09

Alex