Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What maximum size of static arrays are allowed in C?

Tags:

arrays

c

stack

max

In my algorithm I know work with static arrays, no dynamic ones. But I sometimes reach the limit of the stack. Am I right, that static arrays are stored to the stack?

Which parameters affect my maximum stack size for one C programm?

Are there many system parameters which affect the maximal array size? Does the maximunm no. of elements depend of the array type? Does it depend on the total system RAM? Or does every C programm have a static maximum stack size?

like image 231
unlimited101 Avatar asked Feb 07 '13 14:02

unlimited101


3 Answers

Am I right, that static arrays are stored to the stack?

No, static arrays are stored in the static storage area. The automatic ones (i.e. ones declared inside functions, with no static storage specifier) are allocated on the stack.

Which parameters affect my maximum stack size for one C program?

This is system-dependent. On some operating systems you can change stack size programmatically.

Running out of stack space due to automatic storage allocation is a clear sign that you need to reconsider your memory strategy: you should either allocate the buffer in the static storage area if re-entrancy is not an issue, or use dynamic allocation for the largest of your arrays.

like image 154
Sergey Kalinichenko Avatar answered Nov 04 '22 16:11

Sergey Kalinichenko


Actually, it depends on the C compiler for the platform you use.

As an example, there are even systems which don't have a real stack so recursion won't work.

A static array is compiled as a continuous memory area with pointers. The pointers might be two or four bytes in size (or maybe even only one on exotic platforms).

There are platforms which use memory pages which have "near" and "far" pointers which differ in size (and speed, of course). So it could be the case that the pointers representing the array and the objects need to fit into the same memory page.

On embedded systems, static data usually is collected in the memory area which will later be represented by the read-only memory. So your array will have to fit in there.

On platforms which run arbitrary applications, RAM is the limiting factor if none of the above applies.

like image 31
class stacker Avatar answered Nov 04 '22 15:11

class stacker


Most of your questions have been answered, but just to give an answer that made my life a lot easier:

Qualitatively the maximum size of the non-dynamically allocated array depends on the amount of RAM that you have. Also it depends on the type of the array, e.g. an int may be 4 bytes while a double may be 8 bytes (they are also system dependent), thus you will be able to have an array that is double in number of elements if you use int instead of double.

Having said that and keeping in mind that sometimes numbers are indeed important, here is a very noobish code snippet to help you extract the maximum number in your system.

#include <stdio.h>
#include <stdlib.h>

#define UPPER_LIMIT 10000000000000 // a very big number

int main (int argc, const char * argv[])
{
    long int_size = sizeof(int);
    for (int i = 1; i < UPPER_LIMIT; i++)
    {
        int c[i];
        for (int j = 0; j < i; j++)
        {
            c[j] = j;
        }
        printf("You can set the array size at %d, which means %ld bytes. \n", c[i-1], int_size*c[i-1]);
    }    
}

P.S.: It may take a while until you reach your system's maximum and produce the expected Segmentation Fault, so you may want to change the initial value of i to something closer to your system's RAM, expressed in bytes.

like image 42
George Avatar answered Nov 04 '22 16:11

George