I am getting a stack overflow error when I create an instance of my Dungeon class. I feel like it is because my dungeon class creates an array of rooms and my rooms create an array of cells. My only question is I have done this before and never had this issue. So what am I doing wrong this time? Are my arrays too big? I can make my rooms array to a maximum of [5][3] but I would like to have it a [5][5] size room. Is this just impossible to do?
I understand that a stack overflow error is when you run out of memory on the stack but how do I know when I exit that stack and can start fresh again? Or when something gets taken off that stack?
Here is my code on how these 3 classes are setup (Dungeon.h):
#ifndef DUNGEON_H
#define DUNGEON_H
#include <stdlib.h>
#include <string>
#include "TextureHandler.h"
#include "Room.h"
using namespace std;
class Dungeon
{
public:
Dungeon();
~Dungeon();
private:
static const int MAX_RM_ROWS = 5; //Maximum amount of rows of rooms we can have
static const int MAX_RM_COLS = 5; //Maximum amount of columns of rooms we can have
Room rooms[5][5];
int currentDungeon; //Stores the ID of the current dungeon we are in.
int currentRoomRow; //Stores the row position in the room array for what room the player is in
int currentRoomCol; //Stores the column position in the room array for what room the player is in
protected:
};
#endif
Room.h
#ifndef ROOM_H
#define ROOM_H
#include <stdlib.h>
#include "Cell.h"
using namespace std;
class Room
{
public:
Room();
~Room();
void draw();
void setupCell(int row, int col, float x, float y, float width, float height, bool solid, vector<float> texCoords);
int getMaxRows();
int getMaxCols();
private:
static const int MAX_ROWS = 30;
static const int MAX_COLS = 50;
Cell cells[MAX_ROWS][MAX_COLS];
protected:
};
#endif
Cell.h
#ifndef CELL_H
#define CELL_H
#include <stdlib.h>
#include <vector>
#include "GL\freeglut.h"
using namespace std;
class Cell
{
public:
Cell();
~Cell();
void setup(float x, float y, float width, float height, bool solid, vector<float> texCoords);
void draw();
float getX();
float getY();
float getWidth();
float getHeight();
bool isSolid();
private:
float x;
float y;
float height;
float width;
bool solid;
vector<float> texCoords;
protected:
};
#endif
If you are using windows consider
http://msdn.microsoft.com/en-us/library/tdkhxaks(v=vs.100).aspx
to increase your default stack size
Your arrays are quite big. A single
Dungeon
weights about 3 * sizeof(int) + 5 * 5 * 30 * 50 * sizeof(Cell), which is probably somewhere around 1200000 bytes*, i.e., more than one megabyte. That might be a lot to put on the stack, especially if you put more than one.My suggestion is to get rid of all the limits and use
std::vector
s instead of arrays. That uses the heap for storage, solving your problem and giving you unlimited rooms**!*
sizeof(Cell)
is probably around 32: 16 bytes from four floats, one byte for a bool, and 12-16 bytes for a vector, plus padding to align to 4 bytes boundaries.** Well, limited by the available memory, or the operating system.
Your
Cell
is about 32 bytes (or a bit more, depending on the size of avector<float>
). You have 1500 Cells per Room. You have 25 Rooms per Dungeon. So, you need at least 32 x 1500 x 25 = 1.2 MB per Dungeon. That's quite large, but not usually a major problem if you dynamically allocate the structure. If you try to create a local instance on the stack, then you might easily blow the stack.So, dynamically allocate your Dungeons and you should be OK. Do not create them as local variables.
You might decide that it would be a good idea to hide the copy constructor so that you cannot pass a Dungeon by value, only by reference.
Use the heap for big amounts of data and use libraries for the containers. Have a look at boost and qt for example. For the stack: Your stack objects are destroyed as soon as the containing context is left. Context may be: a function, object lifetime, a simple code block between
{}
Concerning the stack size: On Linux you have usually approx. 8 Mb on Windows 32 Mb But you can change these values, so they might differ on your machine