-3

when i try to create an array of 'gameObject'-s i get a stack overflow exception, any idea what might be the reason? Edit: for an array of 1 it DOES NOT throw an exception, i was mistaken (creating just a 'gameObject' variable is fine)

I know my code is messy, and just all around bad, but i'm fairly new to c++ so please excuse my code :(

Here is my Main.cpp:

int main()
{
using namespace std::literals::chrono_literals;

HWND myconsole = GetConsoleWindow();
HDC mydc = GetDC(myconsole);

bool loop;
loop = false;

std::chrono::steady_clock::time_point start;
std::chrono::steady_clock::time_point end;
std::chrono::duration<float> duration;

gameObject test(mydc, "test.dat");
gameObject objList[100];

test.posX = 200;
test.posY = 10;

std::cout << getCurrentId();

while (true)
{
    start = std::chrono::high_resolution_clock::now();



    if (GetKeyState(VK_DOWN) & 0x8000)
    {
        test.move(0, -3);       
    }

    if (GetKeyState(VK_UP) & 0x8000)
    {
        test.move(0, 3);
    }

    if (GetKeyState(VK_RIGHT) & 0x8000)
    {
        test.move(6, 0);
    }

    if (GetKeyState(VK_SPACE) & 0x8000)
    {
        gameObject shell(mydc, "shell.dat");
        shell.type = 1;
        shell.posX = test.posX + test.l;
        shell.posY = test.posY + test.h;
        objList[getCurrentId()] = shell;
    }

    if (loop == false)
    {
        for (int i = 0; i < 100; i++)
        {
            if (objList[i].type == 1)
            {
                objList[i].move(1, 0);
            }
        }
    }

    if (loop == false)
    {
        loop = true;
    }
    else
    {
        loop = false;
    }

    end = std::chrono::high_resolution_clock::now();
    duration = end - start;

    if (duration < 0.0333s)
    {
        std::this_thread::sleep_for(0.0333s - duration);
    }


}
}

and here is the 'gameObject' class:

class gameObject
{
public:
    gameObject(HDC currentDc, std::string dataFile);
    gameObject();
    ~gameObject();

    void clear();
    void draw();

    void move(int x, int y);

    void loadSprite(std::string spriteName);

    bool collide(gameObject);

    unsigned short h = 1;
    unsigned short l = 1;

    int posX;
    int posY;

    unsigned short type;

    COLORREF spriteData[256][256];

    unsigned short id;




    HDC dc;
};
5
  • 1
    " if (loop == false) { loop = true; } else { loop = false; } " --> loop = !loop; Commented Oct 13, 2018 at 16:16
  • well :D that makes sense, like i said im kinda new Commented Oct 13, 2018 at 16:18
  • What is COLORREF (how large is it?) What's your stack size? Commented Oct 13, 2018 at 16:20
  • COLORREF is a windows type for an rgb definition (i assume 3 bytes) Commented Oct 13, 2018 at 16:26
  • COLORREF is 4 bytes Commented Oct 13, 2018 at 16:47

1 Answer 1

2

You are creating all your objects on the stack gameObject objList[100];, and each of them has a big array in them COLORREF spriteData[256][256];. That's your stack overflow.

Use a std::vector to store your objects.

Sign up to request clarification or add additional context in comments.

6 Comments

hm, but i expect to have quite a few objects at one time, should i store them on the heap instead? also, if i make the array smaller (1 member) then i get an access violation error at this line: if (objList[i].type == 1)
Or if it always stays the initial length, maybe use std::make_unique<gameObject[]>(100).
Yes, the stack is small, you shouldn't store big objects there. If you have an access violation, debug the code to see where you are going out of bounds and why.
@Kivi std::vector will take care of that for you. Just use std::vector.
Yes, but on the heap. Please look at the documentation. Glad you sorted it out!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.