VC + + in the stack overflow error when modifying VC + + default stack size

Source: Internet
Author: User
Tags array length

VC + +, in the stack space to apply for storage of the structure or class object array space, if the array length is too large, resulting in the application of the stack space over or close to 1MB, the program can be compiled through, but can not be executed. Hitting debug mode will pop up as shown in the stack space out of Bounds error dialog box.

As the following class defines:

classcharacter{ Public: Character () {x0=0; Y0=0; X1=0; Y1=0; OffsetX=0; OffsetY=0; }unsignedintX0:Ten; unsignedintY0:Ten; unsignedintX1:Ten; unsignedintY1:Ten;UnsignedintOffsetX:8;UnsignedintOffsetY:8;UnsignedintTexindex:8;};

The Freetypefont class has applied for 65,536 character class arrays, the following code:

classfreetypefont{ Public: Ft_library _library;     Ft_face _face;    unsigned _sysfonttexture; Character _character[ 1<<16];   int_texturewidth; int_textureheight; //A temporary variable that holds the position coordinates of the current font written to the arts and Sciences    int_ystart; int_xstart; //! Font size    int_fontsize; //! The size of the above font is not the pixel size of the font, so a variable is needed to calculate the pixel size of the font    int_fontpixelx; int_fontpixely; BOOL_issymbol; Char_vertexbuffer[1024x768* +];}

Generate the Stack overflow error prompt box:

Add the Stack in project settings to the default is 1M, you can increase to 10M to try.
Project->setting->link:
Select output in the category and enter 0x10000000 in the stack's reserve to try it out. Specific settings

  

Whenever a thread is created, the system retains a stack space area for the thread's stack (each thread has its own stack) and submits some physical memory to the reserved area. By default, the system retains a 1MB address space and submits two pages of memory. However, these default values can be modified by setting the/STACK option for Microsoft's linker when you link the application:

/stack:reserve[,commit]
When you create a stack for a thread, the system retains an area of the address space indicated by the/stack switch of the linked program. However, when you call the CreateThread or _beginthreadex function, you can overload the amount of memory that was originally committed. Both functions have a parameter that can be used to overload the amount of memory that was originally submitted to the stack's address space. If you set this parameter to 0, the system will use the committed stack size value indicated by the/stack switch. The following assumes that we use the default stack size value, which is the reserved area of 1MB, each time we commit a page of memory.

=========================================================== Gorgeous split-line ============================================== =============

Before we explain why, let's take a look at a few parts of the memory that a program compiled by C + + uses is divided into:

1, Stack segment: The compiler automatically allocates the release, the value of the parameter that holds the function, the value of the local variable, and so on. Under Windows, the stack is the data structure that extends to the low address, which is a contiguous area of memory. This sentence means that the top of the stack address and the maximum capacity of the stack is the system pre-defined, in Windows, the size of the stack is 2M (also some 1M, in short, a compile-time determination of the constant), if the requested space exceeds the stack's remaining space, will prompt overflow. Therefore, the space available from the stack is small.

2, Heap area (heap segment): Generally by the programmer allocation release, if the programmer does not release, the program ends may be collected by the system. It is different from the heap in the data structure. A heap is a data structure that extends to a high address and is a discontinuous area of memory. This is because the system is stored with a linked list of free memory address, is naturally discontinuous, and the chain of the list of traversal direction is from the low address to high address. The size of the heap is limited by the valid virtual memory in the computer system. Thus, the space of the heap is more flexible and relatively large.

3. Global zone (static zone) (data segment): The storage area of global variables and static variables is together and released by the system after the program ends. The size of the data area is limited by the system and is generally large.

4, text constant area: the constant string is placed here, the program is released after the end of the system.

5, program code area: the binary code that holds the function body.

In summary, the local variable space is very small, we open a a[1000000] will cause the stack overflow, and the global variable space in the win 32bit can reach 4GB, so does not overflow.

Ways to modify the stack size in VC6.0:

1). Select "Project->setting".
2). Select "Link".
3. Select "Output" in "Category".
4. The size of the stack in "reserve:" in "Stack allocations", for example: 32768

The way to set the stack size in VS2010 is:

1). Select Project, Properties.
2). SELECT Linker.
3. Select "System".
4. In the stack reserve size, the size of the stack, for example: 32768.

VC + + in the stack overflow error when modifying VC + + default stack size

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.