I’ve been developing a programm using SDL library. Everything has been done in Linux and works perfectly, the problem comes when porting to Windows. When I build and run the program crashes (Program stoped working) and closes, I first thought that it had something to do with SDL, but I isolated the error to a line in which I just define a two dimensional array or objects of a class. The class prototipe is defined in a header file like this:
#ifndef PARTICULA_H
#define PARTICULA_H
class particula {
public:
particula();
particula(const particula& orig);
virtual ~particula();
int x,y;
int vx,vy;
int tipo;
int tipo2;
int peso;
int empuje;
bool update;
bool update_temp;
int contador;
int temperatura;
};
#endif
Now, the class constructors defined in its .cpp file
particula::particula() {
vx = 0; vy = 0; tipo = 0; peso = 0; empuje = 0;
update = true; contador = 0; temperatura = 0;
update_temp = true; tipo2 = 0;
}
particula::particula(const particula& orig) {
}
particula::~particula() {
}
Ok, in the main() function, just in the beginning, I define an array of this class:
particula matriz[400][220];
If I build and run, the program crashes, if I comment that line, the program doesn’t crash. It can’t be anything else, I’ve commented the whole main function to find that, so that line is the only thing executing.
What could it be? Am I doing anything wrong?
I think you allocate such a big array on stack, so you get a crash. You wrote this line in main function and I do not see new operator. So you allocate memory for your structure on stack. Stack can’t fit so much data… use new keyword to allocate memomy in heap and do not forget to free it later.
Read this article.