Topic: Dynamic Array of Structures
Author: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Date: 1995/07/18 Raw View
In article <3tl827$elq@ds2.acs.ucalgary.ca>
pmnovosa@acs5.acs.ucalgary.ca (Paul Michael Novosad) writes:
|> I've got a problem here, that seems incredibly simple, but I just
|> can't make it work out. All I want to do is create a dynamic
|> array of structures/classes.
|> I have my basic structure:
|> struct Player
|> {
|> char name[20];
|> BOOL active;
|> };
|> Now in my application class constructor which must create an
|> array of four players, I have the following line:
|> actor = new Player[4];
|> Player *actor is a public member of my application class. Now,
|> to access the structure elements, I had thought I could simply
|> use actor[i]->name, where i is the index, but the compiler
|> wouldn't let me do this.
The compiler is correct. The result of `actor[ i ]' has type
`Player', not type `Player*', so the correct expression would be
`actor[ i ].name' (and the type of this expression is `char*').
|> The only way I could access a member of
|> the actor structure was with the following command:
|> (actor+(sizeof(Player))*i) with i as the array index.
This is surely wrong. Consider that `*(actor + i)' is the exact
equivalent of `actor[ i ]' (by definition). Thus, you can write
either `actor[ i ].name' or `(actor + i)->name' (or `(*(actor +
i)).name'). Multiplying the index by sizeof( Player ) is almost
certain to give you a bounds check (except for i == 0, of course).
--
James Kanze Tel.: (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils en informatique industrielle --
-- Beratung in industrieller Datenverarbeitung
Author: pmnovosa@acs5.acs.ucalgary.ca (Paul Michael Novosad)
Date: 1995/07/08 Raw View
I've got a problem here, that seems incredibly simple, but I just
can't make it work out. All I want to do is create a dynamic
array of structures/classes.
I have my basic structure:
struct Player
{
char name[20];
BOOL active;
};
Now in my application class constructor which must create an
array of four players, I have the following line:
actor = new Player[4];
Player *actor is a public member of my application class. Now,
to access the structure elements, I had thought I could simply
use actor[i]->name, where i is the index, but the compiler
wouldn't let me do this. The only way I could access a member of
the actor structure was with the following command:
(actor+(sizeof(Player))*i) with i as the array index.
To make this look better I created a
Player* Player::operator[](int i)
{
return (this + (sizeof(Player))*i);
}
I should then be able to access an element of actor with an index
i with the following code:
(*actor)[i]->name;
This seemed to be a solution, but the program is a windows App,
and Windows threw me an UNHANDLED EXCEPTION 22, and if I run the
program outside the compiler I get a General Protection Fault,
which would seem to indicate that I have exceeded an array's
boundaries somewhere.
This happens in the Application constructor in a for(i=0;i<4;i++)
when i=3.
Now, I have no clue why my program causes a Protection Fault,
because I do not seem to have crossed any array boundaries. I
also feel like there should be a much easier way to access a
dynamic array of structs. If anyone can tell me what my program
is doing wrong, or a better way to use a dynamic array of
structs, please E-mail me directly:
Pmnovosa@acs.UCalgary.Ca
Author: ebiederm@cse.unl.edu (Eric Biederman)
Date: 1995/07/08 Raw View
pmnovosa@acs5.acs.ucalgary.ca (Paul Michael Novosad) writes:
>I've got a problem here, that seems incredibly simple, but I just
>can't make it work out. All I want to do is create a dynamic
>array of structures/classes.
>I have my basic structure:
>struct Player
>{
> char name[20];
> BOOL active;
>};
>Now in my application class constructor which must create an
>array of four players, I have the following line:
>actor = new Player[4];
>Player *actor is a public member of my application class. Now,
>to access the structure elements, I had thought I could simply
>use actor[i]->name, where i is the index, but the compiler
(*(actor + i))->name
>wouldn't let me do this.
Of course you compiler won't let you do that. You have specified a
specific Player and then treated it as a pointer.
> The only way I could access a member of
>the actor structure was with the following command:
>(actor+(sizeof(Player))*i) with i as the array index.
(actor+(sizerof(Player))*i)->name ==
(*(actor+(sizeof(Player))*i)).name ==
actor[sizeof(Player)*i].name
sizeof(Player)*i would definently be out of bounds,
for an array of four.
>To make this look better I created a
>Player* Player::operator[](int i)
>{
> return (this + (sizeof(Player))*i);
>}
>I should then be able to access an element of actor with an index
>i with the following code:
>(*actor)[i]->name;
>This seemed to be a solution, but the program is a windows App,
>and Windows threw me an UNHANDLED EXCEPTION 22, and if I run the
>program outside the compiler I get a General Protection Fault,
>which would seem to indicate that I have exceeded an array's
>boundaries somewhere.
>This happens in the Application constructor in a for(i=0;i<4;i++)
>when i=3.
Make that: index = i * sizeof(Player)
index = 3 * 24 // gues on sizeof(Player)
actor[72]; //yes that would be way out of bounds.
// say 72 * sizeof(Player) bytes passed your block of allocated
// memory that would be 1728 bytes, about a k/2 off
// where you meant to be
>Now, I have no clue why my program causes a Protection Fault,
Now you doo!
>because I do not seem to have crossed any array boundaries. I
No you left those at the starting gate.
>also feel like there should be a much easier way to access a
>dynamic array of structs. If anyone can tell me what my program
Yes there definently is
>is doing wrong, or a better way to use a dynamic array of
>structs, please E-mail me directly:
Sorry I know this is off topic but the standard says that
actor[i] is equivalent to *(actor +i)
please read this part of the standard.
> Pmnovosa@acs.UCalgary.Ca