I have this code which segfaults when run.
#include <stdlib.h> // size_t
int main()
{
size_t s = 0xFFF;
char a[ s * s ];
a[ 0 ] = 'a';
return 0;
}
When I change the value of variable s to 0xFF it works correctly.
How can I determine the maximum possible size of an array in a portable way?
I use 64 bit linux and code is compiled with
gcc file.c
Update:
I do understand that large objects should be allocated with malloc() and small arrays can be allocated locally on the stack. However I want to know exactly how large these small arrays can be. I also want the solution to be portable.
Also, what if I have some other stuff on stack before my array is allocated? In that case, the maximum size of stack can't be used as the size of my array, because the stack will overflow.