2

I wrote a C++ program in Ubuntu. In main function, I have 2-D array like this:

int main() {
   unsigned long long int s[11000][100];
   // some code to manipulate with s (just for initialization)
   // ...
}

And the program failed to run. After search the web, I know that the size of 2-D array exceeds the default stack size in Ubuntu which is about 8 MB. I tried some suggests to change stack size automatically in my program. So I added some 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");
   }

   // some code to manipulate with s (just for initialization)
   // ...
} // end main

But I still got the segmentation fault (core dumped) error. I also checked the stack size, its size is now 32 MB, 4 times lager than the size of 2-D array. Also try set stack size to RLIM_INFINITY, but failed again. Can anybody help me figure out the reason and the solution? Thank you so much!

14
  • 1
    So... just curious... why do you feel the need to stack allocate this array? Commented Sep 24, 2013 at 17:10
  • 9
    The stack size probably has to be known before the stack is allocated. Commented Sep 24, 2013 at 17:10
  • Suggestion: Try to allocate on heap. If is C++, then use std::vector or std::array. Commented Sep 24, 2013 at 17:11
  • Try to create it as a global array. Commented Sep 24, 2013 at 17:14
  • 1
    That's a ridiculously large array to allocate on the stack, so just move it off the stack. Move it out of main() to file scope, or allocate it off the heap as @Mahesh suggested. Commented Sep 24, 2013 at 17:16

2 Answers 2

5

Given the size of this block of memory, you should instead allocate it with either new[] or malloc and delete[] or free it as appropriate. Or, if you're using C++, you should use std::vector or some other heap-allocated container.

The reason it is still crashing is because it's still trying to allocate more than some limit on the still-limited stack space, before you even try to adjust it. Variables in automatic storage (that is, on the stack) are allocated before the function executes.

Sign up to request clarification or add additional context in comments.

6 Comments

Its not 8MB unless you're platform has an 80-byte unsigned long long. The posted code is 11000 * 10 * sizeofunsigned long long), 880000 bytes for an 8-byte implementation; 880kb. That aside, I concur the stack may well be overflowing, but not because of an 8MB hit.
I'm so sorry, my 2-D array is 11000 x 100, not 11000 x 10.
@user2812045 It was only math. Ultimately I think greyfade is correct. Take some steps to debug this by moving it outside of main, making it static, dynamic allocation, etc. This answer is correct.
Hi WhozCraig, I moved it out of main function, and it run successfully. I debugged and receives this message: Segmentation fault (core dumped)
I also put this line: "unsigned long long int s[11000][100]" down to the end of stack size modification code. But it still didn't work.
|
2

One solution to overcome these type of problems is:

Always declare large arrays globally.

This avoids the problems such as yours since the memory for global variables is allocated in the Initialized Data Segment. For more information see this. Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.