Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?
like image 630
Ryan Avatar asked Apr 15 '16 01:04

Ryan


People also ask

What is bss data segment?

In computer programming, the block starting symbol (abbreviated to . bss or bss) is the portion of an object file, executable, or assembly language code that contains statically allocated variables that are declared but have not been assigned a value yet. It is often referred to as the "bss section" or "bss segment".

What is bss in size?

bss and add almost nothing (about 4–8 bytes for the description) to the executable file size. So the reason for . bss is to have smaller executable, saving space and allowing faster loading of the program, as the loader(startup) can just allocate a bunch of zeros instead of having to copy the data from disk. So .

What is bss in memory layout?

bss segment stands for Block Started by symbol. The bss segment contains the object file where all the statically allocated variables are stored. Here, statically allocated objects are those objects without explicit initialization are initialized with zero value.

What is data bss and .text section in program?

'text' is my code, vector table plus constants. 'data' is for initialized variables, and it counts for RAM and FLASH. The linker allocates the data in FLASH which then is copied from ROM to RAM in the startup code. 'bss' is for the uninitialized data in RAM which is initialized with zero in the startup code.


1 Answers

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
like image 160
zertyz Avatar answered Oct 04 '22 17:10

zertyz