Reputation: 13
The code below works fine if factors is small but when it gets larger I get a segmentation fault.The first instance I have encountered is at a size of ~800.000. This should easily fit into memory or what am I missing? Appreciate any help.
char *fgs_arr [facs().size()],
*true_arr[facs().size()],
*maps_arr[facs().size()];
Upvotes: 1
Views: 118
Reputation: 16576
You are running out of Stack (The place where locally declared memory goes). You'll either need to dynamically declare it (this goes to the heap which is much larger) or increase stack size.
Upvotes: 3
Reputation: 168796
As you have discovered, you are running out of stack space. Switching to new
is inelegant; it promotes memory leaks and other bugs. Try std::vector<char*>
:
std::vector<char*> fgs_arr(facs().size());
std::vector<char*> true_arr(facs().size());
std::vector<char*> maps_arr(facs().size());
Upvotes: 0
Reputation: 5456
Since vla is on the stack, and the stack is (in most case) 1MB, it should not easily fit into memory. Use malloc instead (and don't forget to free).
Upvotes: 3