devil0150 devil0150 - 1 year ago 75
C Question

Will memcpy correctly copy an array allocated with mkl_malloc?

I'm allocating an array with

mkl_malloc
with a
64
alignment, and I need to make a copy of it. But I don't understand how aligning works. Does
mkl_malloc
just add padding at the end so that the total size of the array is a multiple of 64 bytes? Or will there also be padding between array elements?

Will code like the example below copy all of
a
into
b
, or only part of it?

#include <stdio.h>
#include <mkl.h>

int main() {
double *a = mkl_malloc(10 * sizeof(double), 64);

// is this enough memory to include a?
double *b = malloc(10 * sizeof(double));

// will this copy all of a?
memcpy(b, a, 10 * sizeof(double));

mkl_free(a);
free(b);
return 0;
}

Answer Source

Your example memcpy(b, a, 10 * sizeof(double)); will work correctly.

Alignment applies to the start address, not to the size of the reserved memory block.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download