C++ still get segmentation fault (core dumped) error after change stack size with setrlimit -
i wrote c++ program in ubuntu. in main function, have 2-d array this:
int main() { unsigned long long int s[11000][100]; // code manipulate s (just initialization) // ... }
and program failed run. after search web, know size of 2-d array exceeds default stack size in ubuntu 8 mb. tried suggests change stack size automatically in program. added few lines of code:
int main() { unsigned long long int s[11000][100]; const rlim_t kstacksize = 32 * 1024 * 1024; struct rlimit rl; int result; result = getrlimit(rlimit_stack, &rl); if (result == 0) { if (rl.rlim_cur < kstacksize) { rl.rlim_cur = kstacksize; result = setrlimit(rlimit_stack, &rl); if (result != 0) { printf("error\n"); } } else { printf("error\n"); } // code manipulate s (just initialization) // ... } // end main
but still got segmentation fault (core dumped) error. checked stack size, size 32 mb, 4 times lager size of 2-d array. try set stack size rlim_infinity, failed again. can me figure out reason , solution? thank much!
given size of block of memory, should instead allocate either new[]
or malloc
, delete[]
or free
appropriate. or, if you're using c++, should use std::vector
or other heap-allocated container.
the reason still crashing because it's still trying allocate more limit on still-limited stack space, before try adjust it. variables in automatic storage (that is, on stack) allocated before function executes.
Comments
Post a Comment