Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The usage of C++ Smart pointer

Shulou Source: shulou.com Published: 2022-06-02 05:16:59 09月17日 Update

This article mainly explains "the usage of C++ smart pointer", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn the usage of C++ smart pointer.

First, the learning of smart pointers:

1. Memory leak:

On the issue of memory leaks, pointers are usually involved in the topic of dynamic memory allocation; however, when programmers manually allocate heap space (pointers cannot control the life cycle of the heap space referred to,), often when they finish writing programs, programmers accidentally forget to release the amount of memory that has been manually allocated, resulting in constant software Bug (that is, memory leaks).

There is no garbage collection mechanism in the C++ language (unlike the high-level language Java, which has an automatic garbage collection mechanism), so programmers often encounter the situation mentioned above when writing programs, so let's look at an example:

# include

# include

Using namespace std

Class Test

{

Int i

Public:

Test (int I)

{

This- > I = I

}

Int value ()

{

Return i

}

~ Test ()

{

}

}

Int main ()

{

For (int item0; I

Tags: Pointers intelligence functions memory space objects programs clocking operators allocation normal timing features programmers learning that is content cycles garbage manual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Tech Info OPPO Reno NVidia Linux