C + + Learning Note: Dynamic memory (i) Manage memory directly

Source: Internet
Author: User

What: What is dynamic memory?

In C + +, the normal variable (non-static object) is stored in the stack memory, static variables (local static, class static) are stored in static memory, there is another memory pool in the system, this part of the memory for the program to allocate, generally referred to as "free space" or "heap "(heap).

Where & When: Where is dynamic memory used?

In the previous variables, the global object was created at the start of the program, destroyed at the end of the program, the local object is created when it enters the block, destroyed when it leaves the block, and the local static object is created the first time it is used, and is destroyed at the end of the program. But sometimes we need another object whose lifetime is irrelevant to where they were created, and only when explicitly released, these objects are destroyed, which is the dynamic object. Dynamic objects invoke dynamic memory.

How: How do I get started with dynamic memory?

  • Dynamically allocating and initializing objects using new
    • Two types of initialization: default initialization and value initialization
    •  //  First initialization: Default initialization:  string  *ps = new  string ; //  initialize to empty string  int  *ps = new   int ; //  
      // The second method of initialization: value initialization: string New string ;  // string initialized to null int New int ;  // Initialize 0
  • Delete an object using delete
    • The free space allocated by new must be deleted with delete, otherwise the memory will be consumed until the computer restarts. (Memory leak)
    • Delete after delete pointer to undefined, at this time the pointer is called the "empty hanging pointer", the threat of calling an empty hover pointer is equal to calling the wild pointer. (Empty hover pointer is automatically destroyed after leaving scope)
    • Delete can only be used to delete dynamically allocated objects, but in general the compiler does not recognize whether the object is dynamic or static, which needs to be identified by the programmer.
    • You cannot delete the same object two times. Delete pi; After the first removal of Pi, the PI has pointed to an undefined memory, at which point the delete memory affects the undefined free space that the pi points to, and the heap memory is destroyed. (Serious hazard)

Why: Why use dynamic memory?

Reference: [Go] Why use dynamic memory allocation

To reduce the difficulty of managing memory directly by C + +, C++11 introduced the smart pointer feature to optimize this memory management. Continue tomorrow ~ Good Night ~

C + + Learning Note: Dynamic memory (i) Manage memory directly

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.