Originally Posted by
obaid
Pointer is better than Array , i hv a example
A array example:-
Code:
int x[10];
int ans = x[4] + x[5];
Compiler may do something like this:
Code:
lea ebx, x
mov eax, [ebx + 4 *4]
add eax, [ebx + 5 * 4]
mov ans, eax
Note that those 4 * 4 and 5 * 4 are constants that the compiler will calculate during compilation.
if we see in low level...... than we known that, there is a lots of step before adding these two value , first step to calculate the address of x[4] and How its calculate, look , first of all x array hv only hold its first element's address , and formulae of calculate addressing is { x's address + (type size)*(index no) } .
in array example , complier calculate more .
A Pointer Example:-
Code:
int x[10];
int *ptr = &x[4];
int ans = *ptr + *(ptr+1);
Code:
lea ebx, x
lea ebx, [ebx + 4 *4]
mov eax, [ebx]
add eax, [ebx+1 * 4]
mov eax, ans
I don't really see how this is more efficient. Did you have a different processor architecture in mind?
in this example complier calcute the address only one time, b/c ptr already hv the address of 4th index and if we add in address the we hv 5th index.
thats why pointer is better then array.
If the compiler can figure out how to convert array indexes into pointer references, then it can do that for both of the above cases, so there's really no difference. I have not seen a compiler that does badly at this sort of things for the last several years, at least not GCC or Microsoft versions. If you use a different compiler, then I suggest you take a look at the code the compiler generates, and if there's really different results, then you probably want to look at what other compilers you can use.
--
Mats