Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C maximum size in main [duplicate]

Tags:

c

memory

Possible Duplicate:
C programming, why does this large array declaration produce a segmentation fault?

This is my first time here so sorry if I break some rules or if this has been answered before. I recently made a C program in which I had a matrix of

char buff[NR][1024*1024];

I needed NR = 128. So the program would alocate 128MB. This was in main(). I tried it on a few systems with enough memory with no error on compile. At runtime I recieved segmentation fault on all systems. It worked for NR = 7, but not 8. I moved that code outside of main making it global. It didn't crash anymore even for 128. Does anyone know why this happened? The compiler was GCC

like image 910
Iustin Avatar asked Jul 15 '10 12:07

Iustin


People also ask

What is the maximum size of matrix in C?

The maximum size of an array is determined by the amount of memory that a program can access. On a 32-bit system, the maximum amount of memory that can be addressed by a pointer is 2^32 bytes which is 4 gigabytes.

What is the maximum size that an array can hold?

The theoretical maximum Java array size is 2,147,483,647 elements.

What is the stack size in C?

Stacks are temporary memory address spaces used to hold arguments and automatic variables over subprogram invocations. The default size of the main stack is about eight megabytes.

Can array size be increased in C?

Arrays are static so you won't be able to change it's size. You'll need to create the linked list data structure.


3 Answers

The problem is you are overflowing the stack which is typically just a few MB in size (the exact size depends on the system and your compiler options). You could allocate the memory on the heap instead using malloc.

like image 95
Mark Byers Avatar answered Nov 06 '22 14:11

Mark Byers


When you put it in main() it allocates the 128MB on the stack, and the stack is usually limited, with the limit varying from system to system. Some might allow you only 8MB, others as much as you can take -- your limit seems to be 8MB, which is standard for most Linux platforms. If this is a POSIX-like environment, you could try controlling the limit with ulimit -s.

When you take the declaration out of main() and make it static it will end up in the BSS segment (unless you initialize it), and be constrained only by heap space on most systems (which is usually very large and/or unlimited). See http://en.wikipedia.org/wiki/Data_segment

However, if you want it locally and ad hoc, consider allocating the NR megabytes yourself:

#define MB (1024*1024)
char *bufp = malloc(NR*MB)
char *buf[NR];
int i;

for (i = 0; i < NR; i++)
  buf[i] = bufp + i*MB;

You could also allocate each MB chunk separately, but I did it this way in case you want the whole area to be contiguous in memory. Remember to free(bufp) when you're done if you're writing a library of if your program will move on to do something else.

like image 5
integer Avatar answered Nov 06 '22 15:11

integer


Your array is allocated on stack. Stack has limited size (depends on os and linker settings). I think default stack size is approximately 1 megabyte for win32(msvc compiler), and 8 megabytes for linux(gcc). Anything larger than that will cause stack overflow, which will cause instant segfault.

Possible solutions:

  1. Increase stack size in linker settings.
  2. Allocate memory dynamically (using malloc).
  3. Make array global.
  4. Make array static.

Making variable global/static will cause (on compilers I've seen, at least) it to be allocated outside of stack - either in data or code segment, which will not affect stack size.

like image 1
SigTerm Avatar answered Nov 06 '22 16:11

SigTerm