Search code examples
c++arraysstack-overflow

How to get rid of stack overflow error


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

Solution

  • 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::vectors 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.