Bug 16274 - Memory allocation error for arrays on stack
Summary: Memory allocation error for arrays on stack
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 3.2.2
: P1 critical
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-06-29 15:37 UTC by Sourangshu Bhattacharya
Modified: 2007-02-15 12:17 UTC (History)
1 user (show)

See Also:
Host: i386-redhat-linux
Target: i386-redhat-linux
Build: i386-redhat-linux
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 Sourangshu Bhattacharya 2004-06-29 15:37:03 UTC
The following simple c code gives segmentation fault on a GNU-Linux system
(Redhat-9.0) running on a Pentium 4 machine with 512 MB RAM.


Source file : test.c

-----------------------------------------------------
#include<stdio.h>

int main()
{
  int a[1024][1024];
  int b[1024][1024];

  b[0][0]=13;
  
  return 0;
}
--------------------------------------------------------

Compile:

--------------------------------------------------------------------------------

[sourangshu@arkavathi temp]$ gcc -v test.c
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
 /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/cc1 -lang-c -v -D__GNUC__=3
-D__GNUC_MINOR__=2 -D__GNUC_PATCHLEVEL__=2 -D__GXX_ABI_VERSION=102 -D__ELF__
-Dunix -D__gnu_linux__ -Dlinux -D__ELF__ -D__unix__ -D__gnu_linux__ -D__linux__
-D__unix -D__linux -Asystem=posix -D__NO_INLINE__ -D__STDC_HOSTED__=1 -Acpu=i386
-Amachine=i386 -Di386 -D__i386 -D__i386__ -D__tune_i386__ test.c -quiet
-dumpbase test.c -version -o /tmp/ccRbyPuy.s
GNU CPP version 3.2.2 20030222 (Red Hat Linux 3.2.2-5) (cpplib) (i386 Linux/ELF)
GNU C version 3.2.2 20030222 (Red Hat Linux 3.2.2-5) (i386-redhat-linux)
        compiled by GNU C version 3.2.2 20030222 (Red Hat Linux 3.2.2-5).
ignoring nonexistent directory "/usr/i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/include
 /usr/include
End of search list.
 as -V -Qy -o /tmp/ccazr8n2.o /tmp/ccRbyPuy.s
GNU assembler version 2.13.90.0.18 (i386-redhat-linux) using BFD version
2.13.90.0.18 20030206
 /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/collect2 --eh-frame-hdr -m elf_i386
-dynamic-linker /lib/ld-linux.so.2
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crti.o
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/crtbegin.o
-L/usr/lib/gcc-lib/i386-redhat-linux/3.2.2
-L/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../.. /tmp/ccazr8n2.o -lgcc
-lgcc_eh -lc -lgcc -lgcc_eh /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/crtend.o
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crtn.o

-------------------------------------------------------------------------------

Execution :

-------------------------------------------------------------------------------
[sourangshu@arkavathi temp]$ ./a.out
Segmentation fault
[sourangshu@arkavathi temp]$ 
-------------------------------------------------------------------------------

Please see whats the problem.
Thank you.
Regards,
Sourangshu.
Comment 1 Sourangshu Bhattacharya 2004-06-29 16:03:49 UTC
Sorry,

In the original bug report, I forgot to delete the

#include <stdio.h>

But reproducing the bug doesn't need stdio.

Thanks.
Regards,
Sourangshu.

(In reply to comment #0)
> The following simple c code gives segmentation fault on a GNU-Linux system
> (Redhat-9.0) running on a Pentium 4 machine with 512 MB RAM.
> 
> 
> Source file : test.c
> 
> -----------------------------------------------------
> #include<stdio.h>
> 
> int main()
> {
>   int a[1024][1024];
>   int b[1024][1024];
> 
>   b[0][0]=13;
>   
>   return 0;
> }
> --------------------------------------------------------
> 
> Compile:
> 
> --------------------------------------------------------------------------------
> 
> [sourangshu@arkavathi temp]$ gcc -v test.c
> Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
> Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
> --infodir=/usr/share/info --enable-shared --enable-threads=posix
> --disable-checking --with-system-zlib --enable-__cxa_atexit
--host=i386-redhat-linux
> Thread model: posix
> gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
>  /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/cc1 -lang-c -v -D__GNUC__=3
> -D__GNUC_MINOR__=2 -D__GNUC_PATCHLEVEL__=2 -D__GXX_ABI_VERSION=102 -D__ELF__
> -Dunix -D__gnu_linux__ -Dlinux -D__ELF__ -D__unix__ -D__gnu_linux__ -D__linux__
> -D__unix -D__linux -Asystem=posix -D__NO_INLINE__ -D__STDC_HOSTED__=1 -Acpu=i386
> -Amachine=i386 -Di386 -D__i386 -D__i386__ -D__tune_i386__ test.c -quiet
> -dumpbase test.c -version -o /tmp/ccRbyPuy.s
> GNU CPP version 3.2.2 20030222 (Red Hat Linux 3.2.2-5) (cpplib) (i386 Linux/ELF)
> GNU C version 3.2.2 20030222 (Red Hat Linux 3.2.2-5) (i386-redhat-linux)
>         compiled by GNU C version 3.2.2 20030222 (Red Hat Linux 3.2.2-5).
> ignoring nonexistent directory "/usr/i386-redhat-linux/include"
> #include "..." search starts here:
> #include <...> search starts here:
>  /usr/local/include
>  /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/include
>  /usr/include
> End of search list.
>  as -V -Qy -o /tmp/ccazr8n2.o /tmp/ccRbyPuy.s
> GNU assembler version 2.13.90.0.18 (i386-redhat-linux) using BFD version
> 2.13.90.0.18 20030206
>  /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/collect2 --eh-frame-hdr -m elf_i386
> -dynamic-linker /lib/ld-linux.so.2
> /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o
> /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crti.o
> /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/crtbegin.o
> -L/usr/lib/gcc-lib/i386-redhat-linux/3.2.2
> -L/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../.. /tmp/ccazr8n2.o -lgcc
> -lgcc_eh -lc -lgcc -lgcc_eh /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/crtend.o
> /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crtn.o
> 
> -------------------------------------------------------------------------------
> 
> Execution :
> 
> -------------------------------------------------------------------------------
> [sourangshu@arkavathi temp]$ ./a.out
> Segmentation fault
> [sourangshu@arkavathi temp]$ 
> -------------------------------------------------------------------------------
> 
> Please see whats the problem.
> Thank you.
> Regards,
> Sourangshu.
Comment 2 Andrew Pinski 2004-06-29 16:07:44 UTC
Not a bug, you stack limit is too low for the program to run. read the man page for limit  or ulimit 
depending on your shell.
Comment 3 Peter Buske 2007-02-15 12:17:33 UTC
(In reply to comment #0)
> The following simple c code gives segmentation fault on a GNU-Linux system
> (Redhat-9.0) running on a Pentium 4 machine with 512 MB RAM.
> 
> 
> Source file : test.c
> 
> -----------------------------------------------------
> #include<stdio.h>
> 
> int main()
> {
>   int a[1024][1024];
>   int b[1024][1024];
> 
>   b[0][0]=13;
>   
>   return 0;
> }

I had the same error with declaring some
double[10001][13] arrays. the program crashes right at the declaration (no printf at the beginning is executed).
It seems to me that this happens because the static memory is just to small.
I didn't find out how to define its size, but you can solve the problem if you allocate the memory dynamically.

#include <stdlib.h>
int **a=(int**)malloc(sizeof(int)*1024*1024); 
int **bb=(int**)malloc(sizeof(int)*1024*1024);

Unfourtunately i couldn't figure out how to do the correct cast, so this produces a warning on indexation, because the pointer types aren't identicall.

Regards
Petre