Skip to main content
GameDev.net gamedev.net
🔒 Locked

Classes Pointers and Access violations

Started by Black Glacier Games Aug 13, 2006 at 6:10 PM 4 replies 600+ views
Original Post
Black Glacier Games
Black Glacier Games
I'm very confused. I've been working at this problem for days and cant get it. I'm trying to use the -> operator to set a value in a class pointer array defined as bullet *bullets [20]. It compiles fine but when I debug it i come up with a access violation. Here's the code. What's the problem? #include typedef class bullet { public: int alive; int x,y; int speed; int side; }; bullet *bullets[20]; int main() { bullets [1]->alive = 0; bullets [1]->x = 0; bullets [1]->y = 0; bullets [1]->side = 0; bullets [1]->speed = 0; }
-Xtra Nitro Studios-Katsuya Tateno
Zakwayda
Zakwayda
Quote:
Original post by Black Glacier Games
I'm very confused. I've been working at this problem for days and cant get it. I'm trying to use the -> operator to set a value in a class pointer array defined as bullet *bullets [20]. It compiles fine but when I debug it i come up with a access violation. Here's the code. What's the problem?

#include

typedef class bullet
{
public:
int alive;
int x,y;
int speed;
int side;
};

bullet *bullets[20];
int main()
{
bullets [1]->alive = 0;
bullets [1]->x = 0;
bullets [1]->y = 0;
bullets [1]->side = 0;
bullets [1]->speed = 0;
}
bullet* bullets[20] is an array of 20 pointers to bullet objects. You're never assigning any values to these pointers, so their values are undefined. An access violation is one of the possible results of accessing an undefined pointer. (As a side note, you may just have happened to have used an index of 1 in your example, but remember that C++ indexing is zero-based.)

One solution to your problem is to allocate a bullet object for each pointer. I'm not going to give that example though, 'cause I kind of doubt that is really your intent.

If you just want an array of 20 bullets, then you need to do something like this:
bullet bullets[20];int main(){    bullets[1].alive = 0;    bullets[1].x = 0;    bullets[1].y = 0;    bullets[1].side = 0;    bullets[1].speed = 0;}
If you want a dynamically resizable array of bullets, different syntax is required, but in this case you should really just use a std::vector<>.

Also, I'm not sure what the 'typedef' before your class declaration is supposed to do.
OrangyTang
OrangyTang
1.
bullet* bullets[20];
defines an array of pointers to bullet classes, not bullets themselves. Also, the initial contents are undefined (ie. they don't even point at NULL). Therefore you need to 'new' a bullet instance for every position in the array (and delete it later, natch).

2. Arrays start at 0, not 1.

3. Standard practice is to use a struct when you have a class with all public members.

4. You don't need the typedef.

5. If you had a constructor for bullet, you could set the contents to zero automatically.

6. Look up std::vector.

Edit: too slow, see above. [grin]
Zahlman
Zahlman
This is actually the first time I've ever seen "typedef class"; I had to stare at that for a little while. As pointed out, the "typedef struct idiom" is useless in C++.

bballmitch
bballmitch
Quote:
Original post by Black Glacier Games
I'm very confused. I've been working at this problem for days and cant get it. I'm trying to use the -> operator to set a value in a class pointer array defined as bullet *bullets [20]. It compiles fine but when I debug it i come up with a access violation. Here's the code. What's the problem?

#include

typedef class bullet
{
public:
int alive;
int x,y;
int speed;
int side;
};

bullet *bullets[20];
int main()
{
bullets [1]->alive = 0;
bullets [1]->x = 0;
bullets [1]->y = 0;
bullets [1]->side = 0;
bullets [1]->speed = 0;
}



I think what you want to do is:
bullet **bullets;bullets = new bullet*[20];for(int i = 0; i < 20; i++){    bullets = new bullet();}int main(){bullets[1]->alive = 0;...}

Mitchen Games
Xai
Xai
adding to the last post:

// inside your bullet class have a default constructor and a member functino// that does this: (reset or clear would both be good names)void bullet::reset()  {  alive = 0;  x = 0;  y = 0;  side = 0;  speed = 0;  }bullet **bullets;void init_bullets()  {  bullets = new bullet*[20];  for(int i = 0; i < 20; i++)    {    bullets = new bullet();    }  }int main()  {  init_bullets();  .  .  .  // and whenever you need to reset one - something like  bullets[0]->reset();  }


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.