Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How large is the attributes can a class object hold? how to determine the stack/heap limit?

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"?

like image 999
rnd_nr_gen Avatar asked Feb 26 '23 22:02

rnd_nr_gen


1 Answers

First of all since you've entitled this C++ and not C why are you using arrays? Instead may I suggest vector<double> or, if contiguous memory is causing problems deque<double> which relaxes the constraint on contiguous memory without removing the nearly constant time lookup.

Using vector or deque may also alleviate other seg fault issues which could plague your project at a later date. For instance, overrunning bounds in your array. If you convert to using vector or deque you can use the .at(x) member function to retrieve and set values in your collection. Should you attempt to write out of bounds, that function will throw an error.

like image 96
wheaties Avatar answered Mar 05 '23 18:03

wheaties