C ++ memory allocation methods

Source: Internet
Author: User

In C, memory is divided into five areas: heap, stack, free storage, global/static storage, and constant storage.

Stack is the storage area for variables that are automatically allocated by the compiler when necessary and clear when not needed. The variables are usually local variables and function parameters.

Heap is the memory blocks allocated by new. Their release compilers are not controlled and controlled by our applications. Generally, a new compiler corresponds to a delete. If the programmer does not release the program, the operating system will automatically recycle it after the program is completed.

The free storage zone is the memory blocks allocated by malloc and so on. It is very similar to the heap, but it uses free to end its own life.

In the global/static storage area, global variables and static variables are allocated to the same memory. In the previous C language, global variables were divided into initialized and uninitialized ones, there is no such distinction in C. They share the same memory zone.

Constant storage zone, which is a special storage zone. It stores constants and does not allow modifications (of course, you can modify them by improper means, and there are many methods, in the article "const thinking", I have provided 6 methods)

Clearly differentiate stack and stack

On the bbs, the distinction between heap and stack seems to be an eternal topic. It can be seen that beginners are often confused about this, so I decided to take him first.

First, let's take an example:
Void f () {int * p = new int [5];}

This short sentence contains the heap and stack. When we see new, we should first think that we allocated a heap memory. What about the pointer p? It allocates a stack memory, so this sentence means that the stack memory stores a pointer p pointing to a heap memory. The program will first determine the size of memory allocated in the heap, then call operator new to allocate the memory, then return the first address of the memory, and put it into the stack, the assembly code in VC6 is as follows:

00401028 push 14 h
0040102A call operator new (00401060)
0040102F add esp, 4
00401032 mov dword ptr [ebp-8], eax
00401035 mov eax, dword ptr [ebp-8]
00401038 mov dword ptr [ebp-4], eax

Here, we have not released the memory for simplicity, So how should we release it? Is it delete p? Australia, the error should be "delete [] p" to tell the compiler: I deleted an array and VC6 will release the memory based on the Cookie information.

Well, let's go back to our topic: What is the difference between stack and stack?

The main differences are as follows:
1. Different management methods;
2. Different space sizes;
3. Whether fragments can be generated is different;
4. Different Growth directions;
5. Different allocation methods;
6. Different Allocation Efficiency;

Management Method: For stacks, it is automatically managed by the compiler without manual control. For heaps, the release work is controlled by programmers and memory leak is easily generated.
// This 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.