对象数组和崩溃[重复]

时间:2021-06-18 11:58:43

This question already has an answer here:

这个问题在这里已有答案:

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:

我一直在使用SDL库开发一个程序。一切都在Linux中完成并且运行良好,移植到Windows时会出现问题。当我构建并运行程序崩溃(程序停止工作)并关闭时,我首先想到它与SDL有关,但我将错误隔离到一行,其中我只定义了一个二维数组或一个类的对象。类prototipe在头文件中定义,如下所示:

#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

现在,类构造函数在其.cpp文件中定义

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:

好的,在main()函数中,就在开头,我定义了这个类的数组:

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?

如果我构建并运行,程序崩溃,如果我评论该行,程序不会崩溃。它不能是其他任何东西,我已经评论了整个主函数来找到它,因此该行是唯一执行的东西。会是什么呢?我做错了吗?

2 个解决方案

#1


2  

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.

我认为你在堆栈上分配这么大的数组,所以你会崩溃。你在main函数中写了这一行,我没有看到新的运算符。所以你在堆栈上为你的结构分配内存。堆栈无法容纳如此多的数据...使用new关键字在堆中分配memomy并且不要忘记稍后释放它。阅读这篇文章。

#2


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:

Windows上的最大堆栈空间量默认为1mb,但该阵列大约需要4mb。所以你必须在堆上分配它:

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

Or:

vector<vector<particula>> matriz;

And then just add the elements as needed.

然后根据需要添加元素。

#1


2  

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.

我认为你在堆栈上分配这么大的数组,所以你会崩溃。你在main函数中写了这一行,我没有看到新的运算符。所以你在堆栈上为你的结构分配内存。堆栈无法容纳如此多的数据...使用new关键字在堆中分配memomy并且不要忘记稍后释放它。阅读这篇文章。

#2


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:

Windows上的最大堆栈空间量默认为1mb,但该阵列大约需要4mb。所以你必须在堆上分配它:

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

Or:

vector<vector<particula>> matriz;

And then just add the elements as needed.

然后根据需要添加元素。