memcpy a buffer and an array not working

17,834

Solution 1

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.

Solution 2

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.

Solution 3

Last parameter of memcpy is number of bytes to copy, so it should be memcpy(a,b, 10 * sizeof(int));

Solution 4

You have to include <string.h>

Last param of memcpy is "Number of bytes to copy". (you are passing number of elements)

Share:
17,834
Zax
Author by

Zax

Work Experience: -&gt;Android (SDK and NDK). -&gt;Video Codec Integration (in FFMPEG). -&gt;ASP.Net MVC-4 (Web Application Development).

Updated on June 15, 2022

Comments

  • Zax
    Zax about 2 years

    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.

  • Zax
    Zax about 11 years
    Perfect.... Thanks a lot working perfectly fine. I'll accept your answer in 10 minutes. But why is string.h required?
  • cnicutar
    cnicutar about 11 years
    @Zax For the memcpy prototype.
  • Zax
    Zax about 11 years
    including string.h even removed the warning. Thanks.