|
From: <or...@vp...> - 2003-08-04 21:09:57
|
Abhijit Shylanath wrote:
>>Hi!
>>
>>Again, a question. Suppose I have a structure like this:
>>
>> typedef struct _block{
>> int array[16][16];
>> int v1;
>> int v2;
>> }block;
>>
>>So, my question is, is that array[16][16] stored linearly in the
>>structure? Can I do a memcpy directly on it, to copy it elsewhere, like
>>this:
>
>
> M$ says Yes.
>
> From MSDN:
>
> -- cut here --
> int prop[3][4][6];
> ...
> i = prop[2][1][3];
>
> This statement is a more complex reference to an individual element of prop.
> The expression is evaluated as follows:
>
> The first subscript, 2, is multiplied by the size of a 4-by-6 int array and
> added to the pointer value prop. The result points to the third 4-by-6 array
> of prop.
>
> The second subscript, 1, is multiplied by the size of the 6-element int
> array and added to the address represented by prop[2].
>
> Each element of the 6-element array is an int value, so the final subscript,
> 3, is multiplied by the size of an int before it is added to prop[2][1]. The
> resulting pointer addresses the fourth element of the 6-element array.
>
> The indirection operator is applied to the pointer value. The result is the
> int element at that address.
> -- cut here --
So actually, the whole stuff is stored linearly, and the compiler deals
with multiplication, to get the final address in memory, relative to prop.
And another question arose in me, reading the above: multiplication.
Isn't that slow? I mean, if I use a loop, and I do a lot of indexing,
that results in a lot of multiplication, no? Wouldn't it be better then
to just incerement the pointer itself. Like, instead of this:
int i,array[100];
for(i=0;i<100;++i){
DoSomething(array[i]);
}
do this:
int *i,array[100];
for(i=array;i<array+100;++i){
DoSomething(*i);
}
Would it be faster?
--
Greetings,
Balázs
|