Dynamic memory management

Stack memory and heap memory

The declared variables and function names are stack memory, but heap memory is used in dynamic memory management.
Stack memory is relatively compact, so it has large local correlation.
There is a dynamic expansion in the heap during operation, and the release needs to be displayed. new is called at run time.
vector and string are dynamic memory

==Let's take an example to see the use of new==

  1. code_one
#include<iostream>
using namespace std;
int* fun()
{
	int* res = new int(2);
	return  res;
}
int main()
{
	int* y = fun();
	cout << *y << endl;
	return 0;
}
  1. code_two
#include<iostream>
using namespace std;
int* fun()
{
	int res = 2;
	return  &res;
}
int main()
{
	int* y = fun();
	cout << *y << endl;
	return 0;
}

Both codes can output 2; However, the second code is very dangerous. Because new uses heap memory and needs to be released manually, the memory and content pointed to by the pointer are retained before release, so 2 can be output successfully; However, the second code value is stored in the stack memory. After calling the fun() function, the memory of the variable declared in the fun() function will be automatically released and a pointer will be obtained to point to an address. However, if other contents are stored in the same address, the output value will make the new value stored in the memory

  1. The construction of objects is divided into two steps: allocating memory and constructing objects on the allocated memory; Destruction is also divided into two steps

Several forms of new

1. Construct a single object and a single array

#include<iostream>
using namespace std;
int* fun()
{
	int* res = new int(0);//Default initialization
	*res = 2;
	return  res;
}
int main()
{
	/*int* y = fun();
	cout << *y << endl;
	return 0;*/
	int A[5][5];
	int* y = new int[5]{};
	delete[] y;//Indicates that an array is deleted
	cout << typeid(*A).name() << endl;
	return 0;
}

2. nothrow new

New is used to open up memory, but if the memory has been allocated, or the left memory space does not have the continuous memory length we require, the development will fail, and nothrow new can throw an exception

#include<iostream>
#include<new>
using namespace std;

int main()
{
	int* y = new (nothrow) int[5]{};
	if (y == nullptr)
	{
		//...
	}
	cout << typeid(*A).name() << endl;
	return 0;
}

3.placement new

Example: vector objects are processed in dynamic memory, and new elements can be added at the end. However, if there is no memory space continuous with the object in the back when adding elements, and the first element of the vector points to 8000, and there is another object at 8016, then you need to copy the value of the current object to another memory, and then you can add elements in the back. After copying the element to the new memory space, no object is created. A new object will be created only after the new element is added. Generally, the continuous length of the new address space copied to is twice the current length.

placement new means that there is already a piece of memory. There is no need to allocate test memory, but only need to build objects on the known memory.

#include<iostream>
#include<new>
#include<vector>
using namespace std;

int main()
{

	vector<int>* A = new vector<int>{1,2,3};
	cout << (*A)[0] << endl;
	int* y = new (A) int(4);//This is the function of placement new, which eliminates the dynamic allocation of memory and constructs objects on the memory pointed to by A
	cout << (*A)[0]<< endl;
	return 0;
}

new auto

int* y = new auto(3);

new align with object

new can take alignment information into account when opening up memory

#include<iostream>
#include<new>
#include<vector>
using namespace std;

struct alignas(256) Str {};//Align indicates alignment information. The starting address of the structure must be an integer multiple of 256 (hexadecimal)
int main()
{

	Str* A = new Str;//new can consider the alignment information in the structure declaration when opening up memory
	cout << A<< endl;
	return 0;
}

Common usage of delete

Destroy single objects and single arrays

int* ptr = new int;
delete ptr;
int* pptr = new int[5];
delete[] pptr

placement delete

Corresponding to placement new, it only destroys objects but does not return resources to the system

Keywords: C++ Back-end Cpp

Added by argan328 on Thu, 17 Feb 2022 20:12:06 +0200