1

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?

4
  • 2
    sizeof (char) is, by definition, 1. If you want the sizeof there, use sizeof *arr1. Commented Sep 19, 2010 at 18:39
  • Huh? sizeof *arr1 will just return 1. Commented Sep 19, 2010 at 18:42
  • @zvrba: right, but at least it will have some meaning and would still be valid if you changed the types. Writing sizeof(char) does not protect you from bugs if you change the type, and it's 100% useless in all cases because sizeof(char)==1 is part of the definition of the sizeof operator. Commented Sep 19, 2010 at 18:45
  • @zvrba: yes ... until the day when he changes the types of the array: struct something *arr1 = malloc((1024*1024) * sizeof *arr1); Commented Sep 19, 2010 at 18:48

3 Answers 3

4

maybe get rid of the multiplications

size_t bigindex = 0, smallindex = 0;
for (int x = 0; x < 768; x++) //copy each row
{
    memcpy(arr1 + bigindex, arr2 + smallindex, nc);
    bigindex += 1024;
    smallindex += 768;
}

Edit d'oh! use the pointers!

unsigned char *a1 = arr1;
unsigned char *a2 = arr2;
for (int x = 0; x < 768; x++) //copy each row
{
    memcpy(a1, a2, nc);
    a1 += 1024;
    a2 += 768;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this worked! I didn't realize that multiplication would slow it down.
1

Rather than copying the whole contents, perhaps initially allocate that as 768 separate arrays, and then use realloc to extend them instead of copying?

Not sure if it really saves any time in the end to have so many separate calls to malloc() rather that the move loop. But if you are having to copy many times, it might. It also assumes you don't want to further modify the original...

2 Comments

+1 Nice side tracking :) ... but realloc will probably have to copy data anyway
There's a good possibility, but even avoiding some copies might be enough to improve performance. And, it probably would have avoided the expensive multiplications that were the real issue...
0

What do you mean by not fast enough? Did you benchmark this?

I suppose your nc is just a named constant that stands for 768?

There is not much that will go faster if you use builtin memcpy that your compiler provides. The problems that might be existing in your approach:

  • alignment issues
  • your constant nc not being a compile time constant
  • you don't have the correct compiler flags

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.