Sign in to follow this  

Pointers to Pointers to Objects to Pointers to Objects

This topic is 3044 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

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
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

This topic is 3044 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this