Thread: [Dev-C++] arrays in structures
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <or...@vp...> - 2003-08-04 14:33:43
|
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:
memcpy(destination->array,source->array,16*16*sizeof(int));
/* destination*/ /* source*/ /* bytes to copy */
Can I do this?
--
Greetings,
Balázs
|
|
From: Abhijit S. <mu...@gm...> - 2003-08-04 20:35:01
|
> 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 --
Multi-line URL follows:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html
/_clang_multidimensional_arrays.asp
--
Abhijit Shylanath
Homepage: http://mudeth.tripod.com/
|
|
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
|
|
From: Daniel K. O. <dan...@ya...> - 2003-08-04 21:52:20
|
OROSZI Balázs wrote:
> 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?
With a good compiler, both of them should be identical. The question
"which is faster" you can test yourself, using clock() before and after
the calls, and see what you get.
Daniel K. O.
|
|
From: Ioannis V. <iv...@em...> - 2003-08-04 22:55:54
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of OROSZI Balazs
> Sent: Tuesday, August 05, 2003 12:10 AM
> To: dev...@li...
> Subject: Re: [Dev-C++] arrays in structures
>
> >
> > M$ says Yes.
> >
> > From MSDN:
> >
> > -- cut here --
> > int prop[3][4][6];
> > ...
> > i = prop[2][1][3];
So what?
> 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?
No, because the compiler undestands indexing as pointer
addition/subtraction.
So your indxing code above is perceived by the compiler as:
int i,array[100];
for(i=0;i<100;++i){
DoSomething(*(array+i));
}
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
|
|
From: Ioannis V. <iv...@em...> - 2003-08-04 22:40:02
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of OROSZI Bal?zs
> Sent: Monday, August 04, 2003 5:34 PM
> To: dev...@li...
> Subject: [Dev-C++] arrays in structures
>
>
> 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:
>
> memcpy(destination->array,source->array,16*16*sizeof(int));
> /* destination*/ /* source*/ /* bytes to copy */
>
> Can I do this?
Yes. But always be careful all the elements of the array to be in the
"automatic storage" (I assume C here) as it is in your case, and not
something like:
int *array[16];
/* ... */
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
|