7

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?

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
unlimited101
  • 3,653
  • 4
  • 22
  • 41
  • 1
    What do you mean by "static arrays"? Local variables in functions, including arrays, are stored on the stack. – Some programmer dude Feb 07 '13 at 14:07
  • I think you can tell the *linker* how big you want the stack to be, though I'm not sure how that works when you create threads. It's about a megabyte on a desktop machine, though, give or take. – Kerrek SB Feb 07 '13 at 14:08
  • 1
    Also, as Joachim says, "static" doesn't mean what you think. Static arrays in fact don't go on the stack at all and can be as big as you like. – Kerrek SB Feb 07 '13 at 14:09
  • what system/architecture are you using, linux, OSX, windows, ARM? could you post a small snippet of code? – gipi Feb 07 '13 at 14:09
  • 1
    static arrays are not stored in stack, but in data segment – ogzd Feb 07 '13 at 14:09

3 Answers3

10

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.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

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.

class stacker
  • 5,357
  • 2
  • 32
  • 65
1

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.

George
  • 774
  • 2
  • 9
  • 18