6

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)?
Ryan
  • 345
  • 3
  • 11
  • there is no memory associated to thread. Any thread can access any memory in the process. You can pass values and pointers from local stack to other thread ( it is not good idea, by the way, potential memory errors ). – Ivan Apr 15 '16 at 01:19
  • 4
    It's implementation dependent. See http://stackoverflow.com/questions/24904047/maximum-size-of-local-array-variable for local arrays, and http://stackoverflow.com/questions/9386979/the-maximum-size-of-an-array-in-c for other arrays. – Barmar Apr 15 '16 at 01:20
  • 1
    @Barmar: answers for C don't necessarily apply for C++. For example, the C++ Standard's Annex B recommends implementations support at least 262 144 byte objects. Implementations should document their actual limits. (Under 1.8/2, an array is an object for which the array elements are subobjects.) – Tony Delroy Apr 15 '16 at 01:37
  • Oops, didn't realize the first one was C++. I found these with google searches, and it found plenty of other similar questions. – Barmar Apr 15 '16 at 01:38

1 Answers1

1

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
zertyz
  • 601
  • 6
  • 9