Bug 32520 - C/C++ programs segfault at runtime if arrays larger than 8MB are declared.
Summary: C/C++ programs segfault at runtime if arrays larger than 8MB are declared.
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-06-27 05:08 UTC by Itai Seggev
Modified: 2007-06-27 08:59 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Itai Seggev 2007-06-27 05:08:51 UTC
Consider a snippet like the following

-----------Begin Snippet--------------
#define N 2048

int main(int argc; char *argv[])
{
  int  mat[N][N];
,,,
-----------End Snippet--------------

or the slightly snazified C99 style

-----------Begin Snippet--------------
int main(int argc; char *argv[])
{
...
   N = strtol(argv[1], arg_end, 0);
   int  mat[N][N];
,,,
-----------End Snippet--------------

One would expect both of these to compile and run just fine. In fact both compile without any problems, and for small values of N work as expected. However, when the size of the array becomes about 8MB, the programs segfault at run time. I get identical behaviour use both styles of declaring mat. This is not a problem with the rest of the code. If I declare mat as an (**int) or as an *int[N] and use malloc, then the program runs just fine for any value of N (well, at least values of N which run in some reasonable period of time). There is nothing sacred about it being a mutli-dimensional array. I've now reproduced with 1d arrays, it's just that my actual code uses 2d arrays. Also, the size limitation seems to be 8MB. If I use char instead of int, I can make the # of elements larger until I hit 8MB. 

I've had identical results both on my x86 (coppermine) Debian box using Debian builds of gcc 3.3, 4.1 and 4.2, and also on my ppc32 Mac OS X machine with Apple builds of gcc 3.3 and 4.0.x. Thus I think this is an issue in gcc core.
Comment 1 Pawel Sikora 2007-06-27 07:28:28 UTC
the 8MB array overflows stack and gcc has nothing to do here
because stack size is controlled by operating system.
use ulimit -s [stack size in kB] to workaround this problem.
Comment 2 Richard Biener 2007-06-27 08:59:43 UTC
Adjust your available stack size.