I have two arrays of chars, allocated as follows:
unsigned char *arr1 = (unsigned char *)malloc((1024*1024) * sizeof(char));
unsigned char *arr2 = (unsigned char *)malloc((768*768) * sizeof(char));
I would like to copy arr2 into arr1, but preserve the row/column structure. This means that only the first 768 bytes of each of the first 768 rows will be changed in arr1.
I wrote a for loop for this, but it's not fast enough for my needs.
for (int x = 0; x < 768; x++) //copy each row
{
memcpy(arr1+(1024*x),arr2+(768*x), nc);
}
Is there a better solution?
sizeof (char)is, by definition, 1. If you want the sizeof there, usesizeof *arr1.sizeof(char)does not protect you from bugs if you change the type, and it's 100% useless in all cases becausesizeof(char)==1is part of the definition of thesizeofoperator.struct something *arr1 = malloc((1024*1024) * sizeof *arr1);