Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Memory Management Library

C++ Memory Management Library

is a header file in the C++ standard library that contains templates and functions for dynamic memory management.

In C++, memory management is an important concept. Dynamic memory management allows a program to allocate and free memory at runtime, which is very useful when dealing with data structures of uncertain size. However, incorrect memory management can lead to problems such as memory leaks and dangling pointers.

The header provides tools such as smart pointers, helping developers manage dynamic memory more safely.

Smart pointers

Smart pointers are the core content of the header. They are a feature introduced in C++11, used to automatically manage dynamically allocated memory. The main types of smart pointers are:

Example

Use std::unique_ptr

Example

#include

#include

class

MyClass

{

public

:

   

void

doSomething

(

)

{

        std

::

cout

<<

"Doing something"

<<

std

::

endl

;

   

}

}

;

int

main

(

)

{

    std

::

unique_ptr

<

MyClass

myPtr

(

new

MyClass

(

)

)

;

    myPtr

doSomething

(

)

;

// Call member functions using a smart pointer

   

// When main ends, myPtr is destroyed and automatically releases the memory of the MyClass object

   

return

0

;

}

Output result:

Doing something

Use std::shared_ptr

Example

#include

#include

class

MyClass

{

public

:

   

void

doSomething

(

)

{

        std

::

cout

<<

"Doing something"

<<

std

::

endl

;

   

}

}

;

int

main

(

)

{

    std

::

shared_ptr

<

MyClass

myPtr1

(

new

MyClass

(

)

)

;

    std

::

shared_ptr

<

MyClass

myPtr2

=

myPtr1

;

    myPtr1

doSomething

(

)

;

// Call member functions using myPtr1

    myPtr2

doSomething

(

)

;

// Call member functions using myPtr2

   

// The MyClass object's memory is released only when both myPtr1 and myPtr2 are destroyed

   

return

0

;

}

Output result:

Doing something

Doing something

Use std::weak_ptr

std::weak_ptr is usually not used alone, but together with std::shared_ptr to solve the circular reference problem.

Example

#include

#include

class

Node

{

public

:

    std

::

shared_ptr

<

Node

next

;

    std

::

weak_ptr

<

Node

prev

;

    Node

(

)

:

next

(

nullptr

)

, prev

(

)

{

}

}

;

int

main

(

)

{

    std

::

shared_ptr

<

Node

node1

=

std

::

make_shared

<

Node

(

)

;

    std

::

shared_ptr

<

Node

node2

=

std

::

make_shared

<

Node

(

)

;

    node1

next

=

node2

;

    node2

prev

=

node1

;

   

// Circular reference, but weak_ptr avoids the memory leak

   

return

0

;

}

In this example,node1 and node2 forms a circular reference. Because prev Is weak_ptr, when node1 and node2 's shared_ptr is destroyed, the memory they point to is also correctly released.

Allocator

The allocator is another important part of.

Allocators are used to allocate memory for containers; all standard library containers use allocators to handle memory allocation.

std::allocator

std::allocator is the standard allocator, providing basic memory allocation and deallocation functions.

Example

#include

#include

int

main

(

)

{

    std

::

allocator

<

int

alloc

;

   

int

p

=

alloc.

allocate

(

1

)

;

// Allocate memory

    alloc.

construct

(

p,

42

)

;

// Construct an object

    std

::

cout

<<

p

<<

std

::

endl

;

    alloc.

destroy

(

p

)

;

// Destroy an object

    alloc.

deallocate

(

p,

1

)

;

// Free memory

   

return

0

;

}

Other memory management tools

std::align

std::align is used to adjust the alignment of a pointer, ensuring that the allocated memory meets specific alignment requirements.

Example

#include

#include

int

main

(

)

{

    alignas

(

16

)

char

buffer

[

64

]

;

   

void

p

=

buffer

;

   

size_t

space

=

sizeof

(

buffer

)

;

   

void

aligned_ptr

=

std

::

align

(

16

,

sizeof

(

int

)

, p, space

)

;

   

if

(

aligned_ptr

)

{

        std

::

cout

<<

"Memory aligned\n"

;

   

}

else

{

        std

::

cout

<<

"Memory alignment failed\n"

;

   

}

   

return

0

;

}

The header is an important part of the C++ standard library for handling dynamic memory management.

By using smart pointers, such as unique_ptr, shared_ptr and weak_ptr, developers can manage memory more safely and efficiently, avoiding common memory management errors. I hope this article helps beginners better understand and use C++'s header file in your C++ program.