Question

I have a question about struct in C++. Actually, my code was something like this

struct SegmentTree {
    int segmentTree[2500000];

    some functions...
}

When I compiled it, it was giving me a segmentation fault immediately, but when I tried to do the same thing with class, everything worked well. So, my question: Is there some limited number of variables that I can use in a struct, or else, what's the problem?

Was it helpful?

Solution

An array declared like this will be allocated on the stack(same as with local variables for functions). The limit of the number of elements in a static array is defined by your stack size(which can be modified using compiler options). Consider using dynamically allocated array(or even better std::vector) to make use of the heap instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top