C ++ new stack, new

Source: Internet
Author: User

C ++ new stack, new

If the 32-bit Windows system has 2 GB of user space by default, the number of new users cannot exceed 2 GB. Run the following code:

double *p = new double[128*1024*1024*2];

The following error occurs:

Error C2148: the total size of the array cannot exceed 0x7fffffff bytes.

That is to say, the total size of the array cannot exceed 2 GB, but it is actually less than 2 GB. If you execute the following statement, the following error will occur:

double *p = new double[128*1024*(1024*2 -120)];

The following error is reported in the debug version.

The following error is reported in the release version:

In addition, the maximum space allocated by the stack in the release and debug modes is also different. After testing, the release mode requires an additional MB space, and the dubug requires MB space, which is used to allocate code segments of the program, in the static zone, there should be some link libraries for the default stacks of processes and threads. The difference between the debug and release modes may be that the versions of the link libraries are inconsistent. This is your own understanding and you are not sure about it.

 

In fact, the new malloc, The HeapAlloc and the VirtualAlloc of the last malloc call on Windows.
VirtualAlloc can only apply for memory in a relatively large unit (minimum 4 kb. HeapAlloc can apply for memory of any size. Basically, malloc can be regarded as calling HeapAlloc directly (basically ). After new calls malloc, constructor is called if it is a class.

 

The above is the stack. Next we will talk about the stack.

Each thread has an independent stack with A default size of 1 MB. the stack is mainly used to store local variables and function parameters. For example, calling another function B in function, the variables in these two functions share one stack. When function B is started, the parameters in function B are used to perform the stack operation. After the result is returned, the stack is output, in this case, if the parameter memory is too large or the recursive call is too deep, the stack data will exceed the stack size, resulting in stack overflow. This is the case in vs debug mode, however, in the release mode, no error is reported if the data allocated exceeds the stack size, and the actual memory does not change. It is strange.

Of course, we can modify the size of the stack space. In vs, we can modify the property-> linker-> system-> stack retention size, in bytes, if the value is 0, the default space is 1 MB.

In addition, the method of clearing the function stack determines that when the function call ends, the call function or the called function clears the function frame. There are two methods for clearing the function stack in VC:

  Parameter transfer order Who is responsible for clearing the stack occupied by parameters?
_ Stdcall From right to left Called function
_ Cdecl From right to left Caller

 

Stack and stack differences

 

1. memory space

STACK: in Windows, a stack is a continuous memory area that expands the data structure to a low address. This statement indicates that the stack top address and maximum stack capacity are pre-defined by the system. In WINDOWS, the stack size is 2 MB (OR 1 MB, in short, a constant is determined during compilation. If the requested space exceeds the remaining space of the stack, overflow is displayed. Therefore, the space available from the stack is small.

Heap: the heap expands the data structure to the high address, and is a non-sequential memory area. This is because the system uses a linked list to store idle memory addresses, which is naturally discontinuous, And the traversal direction of the linked list is from low address to high address. The heap size is limited by the valid virtual memory in the computer system. It can be seen that the heap space is flexible and large.

2. Allocation speed

STACK: as long as the remaining space of the stack is larger than the requested space, you only need to move the top pointer of the stack to complete the allocation. Otherwise, an exception will be reported, prompting stack overflow.
Heap: First, you should know that the operating system has a linked list of idle memory addresses. When the system receives the application, it will traverse the linked list to find the first heap node with a larger space than the requested space, delete the node from the idle node linked list and allocate the Node space to the program. In addition, most systems will record the allocation size at the first address in the memory space, in this way, the delete statement in the code can correctly release the memory space. In addition, because the size of the heap node is not necessarily equal to the applied size, the system automatically places the excess node in the idle linked list.

To open up heap space, you must use system functions to directly modify the pointer on the stack.

 

There are three memory allocation methods:

1. allocated from the static storage area. The internal program has been allocated during compilation, and the internal program exists throughout the runtime. For example, global variables and static variables.

2. Create a stack. When executing a function, all local variable storage units in the function can be created on the stack. When the function is executed, these storage units are automatically released. Stack memory allocation computation is built into the processor instruction set, which is highly efficient but has limited memory allocation capacity.

3. Allocate from the stack, also known as dynamic memory allocation. When the program runs, it uses malloc or new to request any amount of memory. The programmer is responsible for releasing the memory with free or delete. The lifetime of the dynamic memory is determined by us. It is very flexible to use, but it has the most problems.

 

Reference: http://www.cnblogs.com/yyxt/archive/2015/02/02/4268304.html

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.