Set up stack space for C + + programs to resolve stack overflow issues

Source: Internet
Author: User

To set the stack space for C + + programs to solve stack overflow problem when the static data volume of the program is large, sometimes stack overflow problem, often the program has not run the algorithm, then down, such as when you create a large array of classes (or data), the stack will overflow. This is because the stack space that the system allocates to the program is too small. One way to do this is not to statically allocate it, create it dynamically with new, allocate it from the heap, the heap is large enough, but remember to write the destructor, delete the heap space you requested. In fact, this is also very convenient, the end of the class will automatically call the destructor to free space. It is important to develop a good habit of "not defining large arrays/large objects on the stack", otherwise the large stacks will be blown up.   Of course, if you do not like New,delete, or static allocation (after all, static allocation has a lot of benefits), you can change the default stack space to solve. Link's/stack option/stack:reserve[,commit] Reserve: Stack total size commit: The actual amount of memory provided by the system at the start of the program defaults: the 1m,8k parameter is 0 to take the default value today in Vc. NET run the cluster program, always say Stack OverFlow, later found that the stack space is too small.  The amount of data stored on only 100 pages is relatively large.   The size of the stack has been set to: Stack reserve size: 100000000; The stack commit size is: 100000000;   There's no problem. Settings: Project---Properties---linker--system-> stack reserve size/Stack commit size problem answer:

Method One: STACKSIZE define the. def file

Syntax: STACKSIZE Reserve[,commit] Reserve: The size of the stack; commit: optional, related to the operating system, only allocates the size of the physical memory once on NT

Method Two: Set/stack

Open the project and proceed to the following menu: Project->setting->link, select output in category, then set the maximum and commit of the stack in the reserve.

Note: The default value of the reserve is 1MB, the minimum value is 4byte;commit is kept in the virtual memory of the page file, it is set larger will make the stack open a larger value, may increase the memory overhead and startup time

Set up stack space for C + + programs to resolve stack overflow issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.