I have a couple of arrays in my main.cpp, as seen here:
...
Element* screen, *buffer;
screen = new Element[GAME_WIDTH * GAME_HEIGHT];
buffer = new Element[GAME_WIDTH * GAME_HEIGHT];
memset(screen, 0, GAME_WIDTH * GAME_HEIGHT * sizeof(Element));
memset(buffer, 0, GAME_WIDTH * GAME_HEIGHT * sizeof(Element));
...
And, in a my elem.cpp there is a function that uses one of these arrays:
void drawElements(Element* screen[GAME_WIDTH * GAME_HEIGHT]) {
for (int x = 1; x < GAME_WIDTH - 2; x++) {
for (int y = 1; y < GAME_HEIGHT - 2; y++) {
std::cout << screen[idx(x, y)]->id << std::endl; //Temporary, problem here
}
}
}
While all this should do is just print 0 a bunch of times currently, instead it throws the exception shown in the title of this question when debugged, right where the comment is in the elem.cpp code snippet. I've read that this can be caused by not initializing an object, but I think that they are initialized, as they are created and all set to 0 in the main.cpp code snippet.
I'm fairly new to pointers and such, so it is entirely possible that this problem arises from some simple quirk of pointers and references, but i'm not quite sure what is going on.
Here is the definition of the Element struct:
struct Element {
int id;
float lifetime;
int density;
};
And for he who requested it, here is my attempt at a minimal reproducible example of my problem, it throws the same exception when ran through the VC++ debugger.
struct Broken {
int x = 20;
};
void doSomething(Broken* borked[10000]) {
for (int x = 1; x < 10000 - 1; x++) {
std::cout << borked[x]->x << std::endl; //Throws exception here
}
}
int main()
{
Broken* borked;
borked = new Broken[10000];
memset(borked, 0, 10000 * sizeof(Broken));
doSomething(&borked);
}