The usage of C++ Smart pointer
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