Build a sense of hierarchy in C + + memory management

Source: Internet
Author: User

The most powerful aspect of C + + in comparison to other languages is the ability of programmers to control memory and other resources, especially by using standard library containers to selectively automate memory management. However, for C + + beginners, when it comes to memory management, the brain flashes out how new/delete is used, how to prevent memory leaks, that's all, and the rest, a blank.

In fact, C + + on the memory of the control can have a higher level, those new/delete is just a variety of different levels of the lower layer. See what you know:


1. The operating system provides the most basic memory allocation service. This underlying allocation strategy and its characteristics may vary greatly depending on the operating system platform, and this layer of memory management is most likely to be subject to hardware-related constraints.

2. The compiler's default run-time library also establishes its own memory allocation service. This layer of practice, usually one-time from the operating system kernel to apply for a large chunk of memory, and then "retail" to its users, that is, its top "customers."

3. STL containers and allocator then utilize the interface of the runtime library to implement their own policies and optimizations. In addition, we can create our own memory allocator, that is, our own memory pool. You can let the STL container not use the default allocator, and use your own defined memory pool to allocate memory.

Finally, if you never use a memory pool or a memory allocator and use New/delete directly to manage your own memory, this behavior also belongs to the call on top of the runtime library. You have not done any optimization management, your allocation efficiency depends entirely on the implementation of the runtime library.

With a sense of memory allocation hierarchy, I believe it will make you more aware of how you have experienced bumps and get memory, and you can subconsciously think about whether memory management is optimal in your code.

Build a sense of hierarchy in C + + memory management

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.