C + + memory allocation

Source: Internet
Author: User

0.

Learning C++,c#,java is inseparable from understanding the memory allocation theory. If you don't understand how memory is allocated, you need to remember a lot of complicated and awkward rules. If you spend a little bit of time understanding how memory is allocated, then you don't have to memorize the rules that are confusing to the mind.

The rules for Java memory allocation are described in the video of the school horse soldier Teacher. Now let's take a look at the memory allocation rules for C + +.

1. Basic knowledge

Most of our programmers write programs that run on the operating system first. The operating system gives the program some memory space. A C + + program divides these spaces into several zones:

1. Code Area:

2. Global Data area:

3. Heap: Heap Area

4. Stack: Stack area

5. Literal constant Area: coment

Let's discuss the following separately:

1. Code area: The binary code that stores the function.

2. The data area:static variables and global variables defined outside the function are all open to storage in the data area. Uninitialized global variables and static variables are stored in the BBS area. The experiment in vs2012 shows that if the static variable is not initialized manually, the compilation can pass, but the link will be error-able. The way to initialize is outside the main function, which is the shape, int class::member = 0;.

3. Heap area: Heap, can be regarded as a resource pool, we can take things from the resource pool, and then put back. We use malloc, calloc, realloc These applications for memory, and the memory requested is obtained from the heap. The amount of space the heap can apply for is very large. After testing, under VS2012, a Win32 program can request a space of 2G, because the 32-bit addressable space is only 2 G. Frequent malloc and free are likely to produce memory fragmentation (similar to disk fragmentation). When the user requests the memory, the heap first attempts to find the allocated free memory in its own space, if not, then applies to the system and then consolidates the requested resources into its own space before returning to the user. The minimum unit of operating System management memory is the page, which is also allocated when the heap requests resources from the system. The resources the user requests from the heap must be released accurately. The good programming habit is that new and delete pairs appear, and once you write new, write down the delete in the next line.

4. Stack area: Stack, on two operations, into the stack and out of the stack. When you call a function, it presses into a certain amount of space in the stack. Wait until the function is finished and then rewind the stack and return the space back. In memory is made up of high address to low address. The machine will have a special register storage stack address will also have a special machine instruction to operate the stack, so the operation of the stack is relatively fast. If the stack is constantly pressed into the element, the system will error when the memory is eaten up. The size of the stack in Windows is 2M (or 1M, controversial), and this size is pre-set by the compiler. When the function starts in the stack order: stack top pointer, parameter, local variable. The order of the stack is reversed in this order. With the assembly language of C + + translation, we can see that using local variables in the stack is faster than using the heap.

5. Coment area: literal constant. Stores the string.

2. Memory allocation

1. Code area. When the program is compiled, the memory is already allocated, and the global and static variables are already addressed.

2. Stack area. When the program runs, there are corresponding machine instructions and registers to handle, so the speed is very fast.

3. Heap area. Users themselves to apply for their own maintenance. Users need to use the malloc function or new functions or keywords to request/free memory. Problems such as memory leaks can occur if used improperly.

3. Off-topic: comparison between New/delete and Malloc/free

Both the new and MALLOC functions request memory from the heap space.

New invokes the constructor of the class, and malloc accepts a unsigned long value, giving it such a large space.

Delete will call the destructor; Free space.

P.s. Stacks are always connected, because the heap is growing from a low address to a high address, and the stack is reversed from a high address to an address. So always, pile up the back stack first. The study of the underlying machine code reveals that the stack is the data structure provided by the machine system, while the heap is provided by the C/S function library . the efficiency of the heap is lower than the efficiency of the stack.

Reference documents:

C + + memory allocation method, heap area, stack area, New/delete/malloc/free http://blog.sciencenet.cn/blog-268057-366795.html

The static area literal constant area code area http://www.cnblogs.com/chenleiustc/archive/2011/04/08/2009994.html of the stack area

C + + memory allocation

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.