问题描述
我有一个需要大量内存的类。
class BigClass {
public:
BigClass(){
bf1 [96000000-1] = 1;
}
double bf1 [96000000];
};
我只能通过new堆内存中的对象来启动类。
BigClass * c = new BigClass();
assert(c-> bf1 [96000000-1] == 1);
delete c;
如果我在没有new的情况下启动它。我将在运行时得到一个分段错误。
BigClass c; // SIGSEGV!
如何确定内存限制?或者我应该更好地总是使用新?
堆栈有固定的大小,这取决于编译器选项。查看你的编译器文档来改变可执行文件的堆栈大小。
无论如何,对于大对象,喜欢使用新的或更好的:智能指针,如shared_pointer(从boost或从std :: tr1或std ::如果你有非常新的编译器)。I have a class which requiring a large amount of memory.
class BigClass {
public:
BigClass() {
bf1[96000000-1] = 1;
}
double bf1[96000000];
};
I can only initiate the class by "new" a object in heap memory.
BigClass *c = new BigClass();
assert( c->bf1[96000000-1] == 1 );
delete c;
If I initiate it without "new". I will get a segmentation fault in runtime.
BigClass c; // SIGSEGV!
How can I determine the memory limit? or should I better always use "new"?
The stack have a fixed size that is dependant on the compiler options. See your compiler documentation to change the stack size for your executable.
Anyway, for big objects, prefer using new or better : smart pointers like shared_pointer (from boost or from std::tr1 or std:: if you have very recent compiler).
这篇关于类对象可以拥有多大的属性?如何确定堆栈/堆限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!