Both x and xPlus1 should be of type double*. They are pointers to an array of n doubles.
So when you do this:
memcpy(x, xPlus1, sizeof(x));
Since x is just a double*, that only copies sizeof(double*) bytes... which is to say, 8. What you want to do is:
memcpy(x, xPlus1, n * sizeof(*x));
because the type of *x is double, so n * sizeof(*x) will be the total bytes of memory that xPlus1 owns: n doubles.
Both x and xPlus1 should be of type double*. They are pointers to an array of n doubles.
So when you do this:
memcpy(x, xPlus1, sizeof(x));
Since x is just a double*, that only copies sizeof(double*) bytes... which is to say, 8. What you want to do is:
memcpy(x, xPlus1, n * sizeof(*x));
because the type of *x is double, so n * sizeof(*x) will be the total bytes of memory that xPlus1 owns: n doubles.
You left out the declarations.
Adding the type,
double* x = (double*) malloc(sizeof(double)*n);
makes it clearer that x is a pointer to a double.
In this particular case, the double x points to is the first element in an array of n doubles.
Something like this:
(------- n doubles -----)
___________________________
x ===>| | | | | | | | | | | | | |
---------------------------
&x would give you the address of the variable x itself. This is very different from the address that the variable x contains.
(If you have int x = 1;, &x will most likely not be 1.)
memcpy takes two pointers, one for the destination and one for the source, and the size of the memory to copy.
But sizeof(x) doesn't give you the size of the array you allocated; it gives you the size of x, which is a pointer.
(Size is measured in multiples of sizeof(char), which by definition is 1, and sizeof(double*) will be 4 (32 bits) or 8 (64 bits) on most modern machines. So you're copying 32 or 64 bits.)
The size parameter should be the same size that you passed to malloc, if you want to copy the whole thing.
The pointers should be the ones returned from malloc, since these are the memory blocks you want to copy.
So,
memcpy(x, xPlus1, sizeof(double) * n);
should work as expected.
Is there a way to memcpy an array data to another array data with specific location.
c++ - Does memcpy require pointer to array rather than first element? - Stack Overflow
c - memcpy and pointers - Stack Overflow
arrays - Using memcpy in C++ - Stack Overflow
For example if I have array data int x[3] ={4,5,6}; and another int y[8] = {1,2,3,0,0,0,7,8}; can I copy array data x onto array data y and the start location is y[3] with ending location y[5].
f2_int_ptr() needs to become this:
void f2_int_ptr() {
int i = 0;
void *elements = malloc(size * sizeof(int*));
for ( i = 0; i < size; i++ ) {
int *v = malloc ( sizeof(int));
memcpy ( v, &i, sizeof ( int ));
memcpy ( INDEX(i) , &v, sizeof (int*));
}
for ( i = 0; i < size; i++ ) {
int *v;
memcpy ( &v, INDEX(i), sizeof(int *));
printf ( "%d\n", *v );
}
}
Note the subtle changes to the memcpy() arguments.
Note: I really, really, really, wouldn't write code like this! It's incredibly difficult to follow.
Code is very ugly, so I dont even know how it should works but: Why are u using &v here:
memcpy ( &v, INDEX(i), sizeof ( int ));
v is pointer itself:
int *v = (int*)0;
So the order is memcpy(destination, source, number_of_bytes).
Therefore, you can place the old data at the beginning of newarr with
memcpy(newarr, arr, 5 * sizeof *arr);
/* sizeof *arr == sizeof arr[0] == sizeof (int) */
or at the end with
memcpy(newarr+1, arr, 5 * sizeof *arr);
Because you know the data type of arr and newarr, pointer arithmetic works. But inside memcpy it doesn't know the type, so it needs to know the number of bytes.
Another alternative is std::copy or std::copy_n.
std::copy_n(arr, 5, newarr);
For fundamental types like int, the bitwise copy done by memcpy will work fine. For actual class instances, you need to use std::copy (or copy_n) so that the class's customized assignment operator will be used.
memcpy(dest,src,size)
dest -to which variable
src - from which variable
size - size of src varible
int* arr = new int[5]; //source
int* newarr = new int[6]; // destination
for(int i = 0;i<5;i++) {arr[i] = i * 3;printf(" %d ",arr[i]);}
memcpy(newarr,arr,sizeof(int)* 5);
for(int i = 0;i<5;i++) printf("%d",newarr[i]);