1

I have this following very simple code, that works perfectly:

void func(int *tab)
{
    return;
}

int main()
{
    int maxsize = 999*999;
    int tabs[maxsize][6];

    return 0;
}

However, when I modify the main such that I obtain this, it crashes.

int main()
{
    int maxsize = 999*999;
    int tabs[maxsize][6];

    func(tabs[0]);

    return 0;
}

Do you have any idea why? I would really appreciate your help on this, thank you ^^

hmicn
  • 114
  • 9

1 Answers1

2

So although the standard does not talk about stacks most modern implementations will put automatic variables on the stack and the stack will typically between 1M and 8M which you will overflow with your array size. You can find typical stack sizes for different system here:

SunOS/Solaris   8172K bytes
Linux           8172K bytes
Windows         1024K bytes
cygwin          2048K bytes

The reason why the first one does not seg fault is because the compiler does not actually have to reference any memory but if you need to cause some side effect then the compiler generate a memory access which will cause an actually stack overflow. Since you said you are using gcc if I run this code without any side effects(live example) it will indeed adjust the stack pointer but never uses it:

subq    $23952048, %rsp

but if we add a side effect via std::cin and std::cout (live example):

std::cin >> tabs[maxsize-1][5] ;
std::cout << tabs[maxsize-1][5] << std::endl ;

then it will require the use of the stack pointer:

leaq    3(%rsp), %rbx

which will usually generate a seg fault on Unix-like systems.

Note, you may also notice this warning:

warning: ISO C++ forbids variable length array ‘tabs’ [-Wvla]

That is because variable length arrays are not standard C++ (but are valid in C99) are a gcc extension and when using -pedantic it will warn when you are using extensions.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • Just out of curiosity is it kosher to be passing a 2D int array as int *? (Or is this OK on stack, just not with heap?) –  Nov 07 '13 at 21:13
  • @ebyrob It isn't a 2D array that is being passed. A 2D array is a 1D array of 1D arrays. The code is passing the first element in an array of 1D arrays, which is an int* in this case. – janm Nov 07 '13 at 23:18
  • 1
    @ebyrob this a good reference [here](http://stackoverflow.com/questions/12674094/array-to-pointer-decay-and-passing-multidimensional-arrays-to-functions) on array and decaying to pointers, this is one of the better sets of explanations I have found just now. – Shafik Yaghmour Nov 07 '13 at 23:24
  • @janm thanks for responding I was afk but I found a good reference, I wish the c faq covered it better though. – Shafik Yaghmour Nov 07 '13 at 23:25
  • @ShafikYaghmour Thanks! Hmm, that explains a lot of mechanics, but I remember reading something about accessing the "memory block" of m*n directly through a pointer could cause a program crash in some cases (heap, stack, global, various OS). Or is that just referring to not knowing m and n? –  Nov 08 '13 at 14:31
  • @ebyrob that is too vague to answer, code would be needed to understand if it was well behaved or not and that would be outside the scope of this question. But if you come up with a more specific scenario that sounds like something a previous SO question probably has answered already and if not then it would be a new question. – Shafik Yaghmour Nov 08 '13 at 14:45