Jump to content
  • Advertisement
Sign in to follow this  
Guest

Pointers to Pointers to Objects to Pointers to Objects

This topic is 3254 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Guest
For simplicity, assume LEVEL is an object that only contains a pointer to a MAP object which is essentially a dynamically allocated array of ints...which it is. Now, with that in hand and the following definitions:
//In GAME class def
LEVEL** _aryLevels;

//In GAME class ctor
_aryLevels = NULL;

//GAME method
void GAME::AddLevel() {
    (*(_aryLevels + _numLevels)) = new LEVEL[1];
    _numLevels++;
}


I have a few questions: 0) My intent is to create an array of pointers. Is this the correct procedure? 1) How do I fix "Access Violation writing location: 0x00000000"?

Share this post


Link to post
Share on other sites
Advertisement
Try viewing that code in the debugger. You'll see something like this:

// ctor
_aryLevels = 0x00000000;

// GAME method
(*(0x00000000 + 0)) = 0x12345678;


You're writing something to memory adress 0x00000000, which is bad. Allocate some memory and write into that. Note that new LEVEL[1] doesn't create a new LEVEL instance, it allocates a new array for 1 LEVELs, but without any instance being constructed.

Try using a vector, which as a dynamically allocated array:

// class def
std::vector<LEVEL *> _levels;

// ctor
// nothing.

// method
_levels.push_back(new LEVEL());

// dtor
for(int index = _levels.size() - 1; index >= 0; --index) {
delete _levels[index];
}
levels.clear();


Removes all the hard-to-read (LEVEL **) stuff and automatically resizes the level array in bigger steps, thereby improving performance.

Share this post


Link to post
Share on other sites
Quote:
Original post by cugone
0) [...] Is this the correct procedure?
1) How do I fix "Access Violation writing location: 0x00000000"?


You already know the answer of 0) when you look at 1).

[Edited by - phresnel on August 18, 2009 9:59:49 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by cugone

For simplicity, assume LEVEL is an object that only contains a pointer to a MAP object which is essentially a dynamically allocated array of ints...which it is.


For simplicity, let's translate this into code:
struct Level { // all-caps indicates constant or macro, so we capitalize classes
Level(int width, int height)
: map(width, height)
{}
private:
Map map; // we own! the map, 1 map <==> 1 level
}

struct Map {
Map(int width, int height)
: data(width * height)
{}
std::vector<int> data;
};


So now we need an array of variable length of Levels:
struct Game {
std::vector<Level> levels;

void AddLevel() {
Level level;
levels.push_back(level);
}
}


And voila. Isn't this simpler?

Share this post


Link to post
Share on other sites
Quote:
Original post by CygonNote that new LEVEL[1] doesn't create a new LEVEL instance, it allocates a new array for 1 LEVELs, but without any instance being constructed.


Just to clarify, that comment is incorrect. "new LEVEL[1]" does create an instance and it does call the constructor. I think you're thinking of "new LEVEL*[1]" which would create an array (size 1) of pointers to levels, and in that case no instances are created.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!