C++Primer learning notes (11)

The content of this article is dynamic memory.

This piece is a little advanced. It's a part I've rarely touched before.

There are several concerns: global object, local object, local static object and dynamic object.

Refine: static memory, stack memory, free space (heap). Different memory spaces have corresponding objects.

1, Dynamic memory and smart pointer

Are the key points, and every sentence is worth remembering and understanding.  

1.shared_ptr class

 1)make_shared function

Examples:

	shared_ptr<int> sp = make_shared<int>(7);
	cout << *sp << endl;

 2)shared_ Copy and assignment of PTR

About use_ Example of using count

	int s = 20;
	shared_ptr<int> p1 = make_shared<int>(s);//
	auto p= make_shared<int>(7);
	cout << p.use_count() << endl;//
	p = p1;
	cout << p.use_count() << endl;//Original point make_ Shared < int > (7) destroyed

3)shared_ptr automatically destroys managed objects

 4)shared_ptr automatically frees the associated memory

For example:

	auto p= make_shared<int>(7);
	cout << p.use_count() << endl;//Output 1
	vector< shared_ptr<int>> vec;
	vec.emplace_back(p);
	cout << p.use_count() << endl;//Output 2
	vec.pop_back();//Is equal to deleting the last element
	cout << p.use_count() << endl;//Output 1

5) Classes that use dynamic lifetime

Three reasons for using dynamic memory are important and need to be understood

 2. Dynamic memory makes it easy to share data among multiple objects

Next, an example is given to illustrate the understanding of this feature of dynamic memory

1) Define StrBlob class

2) StrBlob constructor

3) element access member function

 4)Str_ Copy, assignment and destruction of Blob

 3. Direct management memory

1) Dynamically allocate and initialize objects using new

	auto s = new auto("123");
	auto c = new auto('a');
	auto i = new auto(2);

2) const object dynamically allocated

const is more complicated.

3) Out of memory

This shows how the pointer handles memory exhaustion.

4) release dynamic memory

5) Pointer and delete value

6) the lifetime of the dynamic object until it is released

7) reset the pointer value after delete

Note: only the space is released, but the pointer still exists. At this time, the pointer is in an empty state.

	auto s = new auto("123");
	delete s;
	cout << *s;//In the suspended state, an error is reported

8) Null pointer and dangling pointer

 4.shared_ptr combined with new

1) Do not mix normal and smart pointers

2) Do not use get to initialize another smart pointer or assign a value to a smart pointer

 

	shared_ptr<int> p(new int(5));
	int* p1 = p.get();//At this point, both p and p1 point to the same space
	cout << *p1 << endl<<p.use_count()<<endl;
	delete p1;
	cout<< p.use_count() << endl;//An error will be reported

3) other share_ptr operation

5. Smart pointer and exception

 ​

1) Smart pointers and dummy classes

For dumb classes, refer to: Dummy classes in C + +_ lenfien blog - CSDN blog

2) Use your own release operation

6.unique_ptr 

Because unique_ptr only allows one pointer to point to the space, so it mainly carries out assignment or pointer transfer through reset and release functions.

1) Pass unique_ptr parameter and return unique_ptr

These two cases can be regarded as unique_ The use of PTR is a special case.

2) to unique_ptr delivery remover

 

The delivery delegator is not clear here, and the example is not obvious enough. To understand it, please refer to: [C++11] exclusive smart pointer unique_ Deletion of PTR_ m0_51955470 blog - CSDN blog 

 7.weak_ptr

Key: lock function

1) Check pointer class

This example doesn't feel very good. First of all, this example is connected with the previous examples, but the previous examples are almost forgotten.  

2) pointer operation

2, Dynamic memory

1.new and array

1) Allocating an array will get a pointer to the element type

2) initialize the array of dynamic allocation objects

Use examples:

void dynamiAarray(int n)
{
	int* p = new int[n] {1,2,3,4};//If n is greater than 4, 0 is added and if n is less than 4, it is truncated. But it's dangerous
    //Cout < < p [2] / / output 3
	for (int* q = p; q != p + n; ++q)cout << *q<<endl;
}
void main()
{
    dynamicarray(2);//Print 1 2
    dynamicarray(6);1 2 3 4 0 0
}

3) it is legal to dynamically allocate an empty array

4) Release dynamic array

Summary: pay attention to square brackets.

5) smart pointer and dynamic array

unique_ptr can easily and safely manage dynamic arrays.

Using share_ptr managing dynamic arrays is quite troublesome and error prone.  

 2.allocator class

1) allocator class

2) allocator allocates unconstructed memory

I think this allocate is a little too troublesome to use.

3) algorithm for copying and filling uninitialized memory

 

 

3, Using the standard library: text query program

This section is to illustrate the whole standard library facility by giving a relatively large example. Do the whole example again later.

Keywords: C++

Added by hkay1 on Tue, 01 Mar 2022 17:08:22 +0200