VS2010 Setting the stack size

Source: Internet
Author: User
Tags constant
VS2010 Setting the stack size

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, is a non-contiguous 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" (for the 32 system, the equivalent of 4 bytes of int a[250000],int) will lead to stack overflow, and the global variable space in the win 32bit can reach 4GB, so does not overflow.


Test VS2010 compiler default set stack space size
#include "stdio.h"
#include <Windows.h>


#define MAX 250000


void Func ()
{
int a[max];//(MAX*4)/1024 size is KByte
}


void Main ()
{
int a = 0,b = 0;
Func ();


System ("pause");
}


Methods for modifying the stack size in the


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


To set the stack size in VS2010:


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


References:


1.http://www.slyar.com/blog/variable-overflow-static.html


2.http://blo g.csdn.net/liuhuiyi/article/details/8207021

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.