When should you increase stack size ( Visual Studio C++ ) [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 months ago.
Improve this question
When should we increase the stack size for a C++ program?
Why isn't it unlimited and are there any reasons for not increasing the stack size?
And does a program crash if the stack is full?
And also can we increase the stack size for a particular thread?

When should we increase the stack size for a C++ program?
When you have a specific program or a use case that overflows the stack. Note that the ideal solution is to modify the program or algorithm to behave within reasonable stack size, but that isn't always possible in practice (e.g. you have a program you cannot modify).
Why isn't it unlimited and are there any reasons for not increasing the stack size?
Because is not possible within the current architecture. In the virtual memory space of a program there are multiple stacks, one for each thread so specific limited space must be reserved for each stack. Keep in mind that a stack cannot be fragmented and cannot move (relative to the virtual memory space).
And does a program crash if the stack is full?
Please forgive my pedantic note: If the stack is full but you don't exceed it there is not problem. The problem is when the program overflows the stack.
I am not sure exactly. I think there is OS level protection against stack overflow in which case the program crashes with stack overflow exception. If am wrong and there is no protection (or if there is a setting to disable it) it depends on what it is in the memory you overflow. In any case, nothing good happens.
Why is the stack size set so small by default?
ok, it's not your question, but I feel it ties in here neatly
It's not. The OSes need to find a balance between too big of a stack and too small. Too big and you cut into the heap memory, too small and you make programs overflow it.
What can reside on the stack: call frames and local variables allocated on the stack. Call frames are very small (they usually contain just 1 pointer per frame) and local variables usually are pretty small. Big objects go on the heap.
What can overflow a stack? The most likely culprit is recursion. A recursive algorithm can easily overflow the stack with a big maximum recursion depth. But every recursive algorithm can be rewritten. Either there is an equivalent iterative algorithm or simply use a stack on the heap instead. That is the reason why in stack based memory allocation languages like C and C++ in the real world recursive algorithms with unbound recursion depths are avoided.

Related

C++ : Use case / real world application of using the heap memory [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 months ago.
Improve this question
I am trying to code a relatively simple program to understand in which scenarios it would be more efficient and useful to use the heap.
I first read that you better store large objects on the heap.
So I created a std::vector on the heap and filled it with an insane amount of bytes, something like 18Gb. At some point, it threw a std::bad_alloc exception, then my OS (Linux mint) killed the process after the swap was full. But the result was the same with the stack, so I don't understand how it's better on the heap.
Maybe I lack creativity, but I cannot think of a design where I would absolutely need to use the heap, I can always pass a reference of my object on the stack and achieve the same memory usage efficiency. For every program I wrote, it was always more efficient to use the stack speed wise and was the same for memory usage.
So, in what scenario is it useful to use the heap regarding memory usage efficiency or speed?
You use the heap whenever you need more flexibility than the stack provides. If you need to allocate memory to return something from a function, then it can't be on the stack, because it would be freed when the function returned. And it can't be global, because you might call the same function more than once.
In C++, you might not always realize that you are using the heap, because classes like std::vector and std::string use the heap internally. You don't always have to write new yourself to use the heap.

What is the correct way to minimize data held on the stack? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Preempt: (if anyone can link helpful articles explaining the stack and heap at a deep level, up to registers, would be much appreciated)
I am new to C++: I am trying to really grasp how memory management works. At this point, I understand any declaration of the type ObjClass obj; to have automatic duration within the scope it's declared in. Yet, ObjClass* obj = new ObjClass(); stores the obj pointer on stack, but assigns it an address of memory on the heap. What I'm wondering about is, in more complex programs, what design pattern is used to prevent stack overflow? I could see the storage on stack quickly exceeding 1mb. Is this achieved by making multiple smaller functions which run, use stack, and then automatically deallocate?
Related Q: As for global variables, I know they are held in "static" storage, yet am unsure how that works in the context of stack/heap. How is their memory allocated, and is there a small limit like the stack? Is heap close to the size of system RAM minus the OS-reserved memory?

Is the HEAP a term for ram, processor memory or BOTH? And how many unions can I allocate for at once? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I was hoping someone had some schooling they could lay down about the whole HEAP and stack ordeal. I am trying to make a program that would attempt to create about 20,000 instances of just one union and if so some day I may want to implement a much larger program. Other than my current project consisting of a maximum of just 20,000 unions stored where ever c++ will allocate them do you think I could up the anti into the millions while retaining a reasonable return speed on function calls, approximately 1,360,000 or so? And how do you think it will handle 20,000?
Heap is an area used for dynamic memory allocation.
It's usually used to allocate space for variable collection size, and/or to allocate a large amount of memory. It's definitely not a CPU register(s).
Besides this I think there is no guarantee what heap is.
This may be RAM, may be processor cache, even HDD storage. Let OS and hardware decide what it will be in particular case.

Memory and performance in C++ Game [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm still new to C++ but I catch on quick and also have experience in C#. Something I want to know is, what are some performance safe actions I can take to ensure that my game runs efficiently. Also, in which scenario am I likely to run out of memory on. 2k to 3k bullet objects on the stack, or heap? I think the stack is generally faster, but I heard that too much causes a stack overflow. That being said, how much is too much exactly?
Sorry for the plethora of questions, I just want to make sure I don't design a game engine in which it relies on good PCs in order to run well.
Firstly, program your game safely and only worry about optimizations like memory layout after profiling & debugging.
That being said, I have to dispel the myth that the stack is faster than the heap. What matters is cache performance.
The stack generally is faster for small quick accesses, because the stack usually already is in the cache. But when you are iterating over thousands of bullet objects on the heap, as long as you store them contiguously (e.g. std::vector, not std::list), everything should be loaded into the cache, and there should be no performance difference.

What's the best way to allocate HUGE amounts of memory? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm allocating 10 GB of RAM for tons of objects that I will need. I want to be able to squeeze every last byte of RAM I can before hitting some problem like null pointer.
I know the allocator returns continuous memory, so if I have scattered memory from other programs, the max continuous size will be quite small (I assume), or smaller than the actual amount of remaining free memory.
Is it better to allocate the entire size of continuous memory I need in one go (10GB) or is it better to allocate smaller non-contiguous chunks and link them together?
Which one is more likely to always return all the memory I need?