rainwarrior wrote:
Do you understand how accessing data in an array is different than simply computing a pointer value?
I'm not sure what exactly you are referring to, but as far as I know, both are interchangable in C.
So, firstly, you can declare
int x[5]; or you can declare
int *x; and use the same operators on each of them.
And secondly, the following comparisons are always true:
&x[3] == x + 3
x[3] == *(x + 3)This is the case, no matter if x is a pointer or an array.
The only two differences are:
1. The declaration. You cannot declare
int *x = { 1, 2, 3 };, it has to be
int x[] = { 1, 2, 3 };).
2. The
sizeof operator returns the actual data type size * array length if the variable was declared as an array. And it only returns data type size if it was declared as a pointer.
So, I know what the difference between [] and * is supposed to be from a logical point of view. But from a technical point of view, they are pretty much identical in C and C++.
You can even declare
int a = 5;
int *b = &a;and then call
b[3] and the compiler will not complain, even though, from a logical standpoint, this pointer isn't involved in anything array-related at all.
So, since
value[3] is identical to
*(value + 3) and since
&value[3] is identical to
value + 3 and since you can do all four operations on pointers and arrays, I was asking whether it's true that one of them internally creates a more efficient assembly code.
rainwarrior wrote:
Also, if the array in question is static, it might be better to just pass an offset index instead of a pointer. If something is static, try not to hide that fact from the compiler if you can.
My CopyArray and FillArray are general purpose functions.
Yes, I know, accessing a global array is easier since the assembly code just needs to do something like
LDA MyArray, Y (or, if even the offset is known,
LDA MyArray + 3) and doesn't need to do indirect addressing like
LDA (MyPointer), Y.
But I do have various arrays that need copying and filling, so unless I want to put the copy and fill functions multiple times into the ROM, one for each array, I have to use pointers.
(And since I have to use pointers for copying arrays and since sometimes the copying starts somewhere in the middle of the array, I was asking whether
pointer = array + offset; is really worse than
pointer = &array[offset].)
I do use a fixed array for shifting, though (i.e. shifting all array items one position to the left), since only one array in the whole game needs that. So, the assembly function accesses this array directly instead of using a pointer.