Ryan
Ryan

Reputation: 345

array max size on the stack or on BSS or DATA segment

In C++, we all know the array can be in the "main" scope as the local variables:

int main(){
    int arr[10000];    //on the stack, size can't be very large
    ....
}

or out of the "main" scope as global variables:

int arr[10000000];     //on BSS, sie can be very large
int main{
    ....
}

but I want more for this problem.

  1. what is the max array size? I mean the exactly value.
  2. What will limit the max size, for stack, I think the answer is the stack when the thread is created. But for BSS, I really don't know what exactly it is, what will limit the size of it, and is it associated with thread(just like stack) or application(like heap)?

Upvotes: 6

Views: 2094

Answers (1)

zertyz
zertyz

Reputation: 705

The stack size for the main thread is allocated by the operating system at process creation time. On linux, you can inspect and change it with the command 'ulimit'. To get a list of current process creation limits:

ulimit -a

On my Linux x64, the default is:

stack size              (kbytes, -s) 8192

If your program creates any threads, each thread will also have their stack size set to a default value (2048k on linux/pthread) which you can change using the function:

int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);

For the BSS size, the limit is how much virtual memory your process can access: 1.5-2g on a 32bit machine and approximately 2^b on a 64bits one. Note that 'b' is not necessarily 64:

cat /proc/cpuinfo

On my old server gives:

address sizes   : 36 bits physical, 48 bits virtual

Upvotes: 1

Related Questions