//In GAME class def
LEVEL** _aryLevels;
//In GAME class ctor
_aryLevels = NULL;
//GAME method
void GAME::AddLevel() {
(*(_aryLevels + _numLevels)) = new LEVEL[1];
_numLevels++;
}
Pointers to Pointers to Objects to Pointers to Objects
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:
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"?
Try viewing that code in the debugger. You'll see something like this:
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:
Removes all the hard-to-read (LEVEL **) stuff and automatically resizes the level array in bigger steps, thereby improving performance.
// 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 defstd::vector<LEVEL *> _levels;// ctor// nothing.// method_levels.push_back(new LEVEL());// dtorfor(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.
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]
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?
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement