Function Comparison of various C ++ memory regions

Source: Internet
Author: User

C ++, as an upgraded version of C language, has very powerful functions. It not only supports various programming styles, but also has all the functions of C language. Here we will introduce you to a more important part, the basic introduction of the C ++ memory area.

  • Introduction to logical partition of C ++ memory
  • How to disable the C ++ heap object
  • How to disable C ++ stack objects
  • Basic concepts of C ++ Functions
  • Discussion on the basic content of the variable parameter table in C ++

The C ++ memory area is divided into five areas. Stack, free storage, global/static storage, and constant storage respectively.

STACK:The variable storage area allocated by the compiler when needed and automatically cleared when not needed. It usually contains local variables and function parameters.

Heap:Memory blocks allocated by new are not managed by the release compiler. They are controlled by our application. Generally, a new block corresponds to a delete. If the programmer does not release the program, the operating system will automatically recycle it after the program is completed.

Free storage zone:Memory blocks allocated by malloc are very similar to the heap, but it uses free to end its own life.

Global/static storage zone:Global variables and static variables are allocated to the same memory in the previous C language. Global variables are divided into initialized and uninitialized ones. They are not distinguished in c ++, and they share the same memory.

Constant storage zone:This is a special storage area where constants are stored and cannot be modified.

The difference between stack and stack in the C ++ memory area:

Different management methodsThe stack is automatically managed by the compiler without manual control. For the stack, the release is done by the programmer, which is prone to memory leakage.

Different space sizes:Generally speaking, under 32 is the system, the heap memory can reach 4 GB. From this perspective, there is almost no limit on the heap memory. But for the stack, there is usually a certain amount of space. For example, under vc6, the default stack size seems to be 1 M. Of course, you can also modify it by yourself: Open the project. Project --> setting --> link, select output in category, and then set the maximum value and commit of the stack in reserve.

Whether fragments can be generated:For the heap, frequent new/delete operations will inevitably lead to memory space disconnections, resulting in a large number of fragments, reducing program efficiency. This problem does not exist for stacks.

Different Growth directions:For the heap, the growth direction is upward, that is, the direction for increasing the memory address; For the stack, the growth mode is downward, and the direction for decreasing the memory address.

Different allocation methods:Stacks are dynamically allocated. stacks are statically and dynamically allocated. Static allocation is completed by the compiler, such as local variable allocation. Dynamic Allocation is implemented by the alloca function, but the dynamic allocation of stacks is different from that of stacks. The dynamic allocation of stacks is released by the compiler without manual implementation.

Different allocation efficiency:The stack is the data structure provided by the machine system. The computer will provide support for the stack at the underlying layer: assign a special register to store the stack address, and all the output stacks of the Pressure stack have special command execution, this determines the high efficiency of the stack. The heap is provided by c/c ++ library functions, and the mechanism is very complicated. Library functions are allocated according to certain algorithms. Obviously, the heap efficiency is much lower than the stack efficiency.

The above is a basic introduction to the C ++ memory area.

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.