We only see a fragment of code, if you are initializing a global array of pointers to point to a global array of uint8_t
, there is a faster way: write an explicit initializer. The initalization is done at compile time and takes virtually no time at execution time.
If the array is automatic, I'm afraid there is no faster way to do this. If your compiler is clever and instructed to use optimizations (-O2, -O3, etc.) it will probably unroll the loop and generate pretty efficient code. Look at the assembly output to verify this. If it does not, you can unroll the loop yourself:
Assuming the array size is a multiple of 4:
for (i = 0; i < sizeof(idx) / sizeof(*idx); i += 4)
idx[i] = idx[i+1] = idx[i+2] = idx[i+3] = &raw[0];
Note that you should be careful with the sizeof operator: in addition to using the wrong array for the size computation, your code makes 2 implicit assumptions:
- The array element is a char
idx
is an array, not a pointer to an array.
It is advisable to use sizeof(idx) / sizeof(*idx)
to compute the number of elements of the array: this expression works for all array element types, but idx
still needs to be an array type. Defining a macro:
#define countof(a) (sizeof(a) / sizeof(*(a)))
Makes it more convenient, but hides the problem if a
is a pointer.