What determines the size of the stack in a stream in C ++?

I use C ++ and Windows.h in my source code. I read the CreateThread API on MSDN , but I still don't understand the essence of determining stack size. The default is 1 MB. But what happens if I specify 32 bytes?

What determines the size of the stack in the stream?

Please provide a detailed explanation and I will be grateful. Thanks.

+5
source share
3 answers

The stack is used to store local variables, pass parameters in function calls, and store return addresses. The stream stack has a fixed size, which is determined when the stream is created. This is the meaning that you also have in mind.

The stack size is determined when the stream is created, since it must occupy the adjacent address space. This means that the entire address space for the thread stack must be reserved at the point where the thread was created.

If the stack is too small, it may overflow. This error condition, known as the stack overflow, from which this website got its name. When you call a function, the following or the following happens:

  • Parameters are pushed onto the stack.
  • The return address is pushed onto the stack.
  • A stack stack is created that contains the space for local function variables.

All this takes up space from the stack. When a function in turn calls another function, more stack space is consumed. As the call stack goes deeper, more stack space is required.

The consequence of this is that setting too large a stack is that you can run out of stack and overflow it. This is the final condition from which it is impossible to recover. Of course, 32 bytes (rounded to one page, which is 4096 bytes) is too small for almost all streams.

If you have a program with a large number of threads, and you know that a thread does not need to reserve 1 MB of stack size, then there may be advantages to using a smaller stack size. This avoids running out of available process address space.

On the other hand, you may have a single-thread program that has deep call stacks that consume a lot of stack space. In this case, you can reserve more than the default value of 1 MB.

However, if you have no good reason for doing this, it is best to stick to the default stack size.

+14
source

The size of the stack is simply a compromise between the ability to create many threads and the ability to overflow the stack in one of them.

The larger the stack, the less threads you can create and the less likely it is. You should worry about the size of the stack only if you are going to create a lot of threads (you will have to reduce the size of the stack, but remember the stack overflow). Otherwise, the default value will be sufficient.

+1
source

But what happens if I specify 32 bytes?

I have not read the Windows documentation, but if Windows allows this (specify only 32 bytes), you will most likely get a stack overflow. According to their documentation, the value is rounded to the page size anyway, so in reality the stack size will be no less than the page size. The created thread assumes that there is enough "stack space" for it (to distribute automatic variables, save function addresses, etc.) and allocate space in accordance with its needs. When there is not enough stack space, the stack allocator may use invalid memory, overriding the memory used elsewhere.

What determines the size of the stack in the stream?

Determines how much memory will be allocated for use by this stream stack.

There is a good description of what exactly the call flow stack is here

0
source

Source: https://habr.com/ru/post/1235948/


All Articles