0

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?

MyUserIsThis
  • 417
  • 1
  • 4
  • 17

2 Answers2

3

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.

Leonidos
  • 10,482
  • 2
  • 28
  • 37
  • hmm. what do I do? use dynamic memory? – MyUserIsThis Feb 09 '13 at 22:32
  • Not sure about the limits for the stack in windows, but that array is slightly over 3M. Not sure that should be enough to run out of stack space... – David Rodríguez - dribeas Feb 09 '13 at 22:40
  • @David Ok, I'll do that, thanks. Do you know how much memory can the stack hold? And why is it that it worked in Linux installed on a laptop 5 years old, and not on a new laptop with Windows and a lot more memory, is it really this system that bad at managing memory? – MyUserIsThis Feb 09 '13 at 22:43
  • It depends on your compiler options, check it's documention. You can increase this value, but it is wrong to allocate big structures on stack. I think that default size is about 1M. – Leonidos Feb 09 '13 at 22:46
  • @MyUserIsThis: In linux you can see the size of the stack with `ulimit` (read the man page for the specific options). AFAIK for a long time the default stack size has been 10M for a very long time. – David Rodríguez - dribeas Feb 09 '13 at 22:51
0

The maximum amount of stack space on Windows is 1mb by default, but that array requires about 4mb. So you have to allocate it on the heap:

particula (*matriz)[220] = new[400][220];

Or:

vector<vector<particula>> matriz;

And then just add the elements as needed.

user1610015
  • 6,561
  • 2
  • 15
  • 18
  • The second approach has a completely different memory footprint (i.e. each row will be allocated in a non-contiguous block of memory). You are better off writting a helper class that allocates a single `vector` and does the coordinate mapping. – David Rodríguez - dribeas Feb 09 '13 at 22:42
  • Yeah, I didn't say they allocated memory in the same way. But either approach will work for the purposes of MyUserIsThis. – user1610015 Feb 09 '13 at 22:57