Memory management in C/C + +

C/C + + memory distribution:

Distribution description: 1. Stack is also called stack. Non static local variables / function parameters / return values, etc. stack grows downward. 2. Memory mapping segment is an efficient I/O mapping method, which is used to load a shared dynamic memory library. Users can use the system interface to create shared memory for inter process communication. 3. Heap is used for dynamic memory allocation during program running. Heap can grow up. 4. Data segment – stores global and static data. 5. Code snippet - executable code / read-only constant.
Dynamic memory management in C language:
1.malloc function: malloc(size_t size) function has one parameter, that is, the size of memory space to be allocated.

void main()
{
	int* ptr = (int*)malloc(sizeof(int) * 10);   //C
	if (ptr == NULL)
		return;

	int* p2 = new int(10);  //C + + new has the function of initialization

	free(ptr);
	delete []p2;
}

2.calloc function: the only difference between calloc and malloc is that calloc initializes each byte of the requested space to all zeros before returning the address.

#include <stdio.h>
#include <stdlib.h> 
int main() {
	int* p = calloc(10, sizeof(int)); 
	if (NULL != p) 
    {
		//Use space
	} free(p); 
	p = NULL; 
	return 0;

3.realloc function: realloc function can adjust the size of dynamic memory.
C + + memory management mode: dynamic memory management through new and delete operators. Apply for and release the space of a single element, use the new and delete operators, apply for and release the continuous space, and use new [] and delete [].

void main()
{
    // Dynamically apply for a space of type int 
    int* ptr4 = new int;
	int* p2 = new int(10);//C++ new has the function of initialization (10)
	// Dynamically apply for 10 int type spaces 
	int* ptr6 = new int[3];
	
	delete ptr4;
    delete p2;
    delete []ptr6;
}

New and delete operation custom type: when applying for a space of custom type, new calls the constructor and delete calls the destructor, but malloc and free do not.

class Test
{
public:
	Test()
	{
		cout << "Create Test Object." << endl;
		m_data = 0;
	}
	~Test()
	{
		cout << "Free Test Object." << endl;
	}
public:
	void InitTest(int data = 0)
	{
		m_data = data;
	}
private:
	int m_data;
};

void main()
{
	int ar[10];
	int a = 10;
	//Apply for 1 Test class space
	Test* pt = new Test[a];

    //Apply for 10 Test spaces
	Test* pt = new Test[10];
	delete[10]pt;

	Test* pt = new Test;  //Apply for space transfer constructor
	delete pt;     //Destroy objects to free up space

Operator new and operator delete functions: new and delete are operators for dynamic memory application and release. Operator new and operator delete are global functions provided by the system. New calls operator new global function at the bottom to apply for space, and delete releases space through operator delete global function at the bottom.

void* operator new(size_t sz, int* ptr, int pos)
{
	return &ptr[pos];
}

void* operator new(size_t sz)
{
	void* ptr = malloc(sz);
	return ptr;
}
void operator delete(void* ptr)
{
	free(ptr);
}

class Test
{
public:
	Test()
	{
		cout << "Test::Test()" << endl;
	}
	~Test()
	{
		cout << "Test::~Test()" << endl;
	}
};
void main()
{
	Test* pt = new Test;  //The new operator operator new is only responsible for opening up space. The return value must be a typeless pointer and a typeless shaping parameter
	Test* pt1 = (Test*)operator new(sizeof(Test));
	//Test *pt=(Test*)::operator new(sizeof(Test));   The two are equivalent
}

operator new actually applies for space through malloc. If malloc successfully applies for space, it will return directly. Otherwise, it will take measures to deal with insufficient space provided by the user. If the user provides this measure, it will continue to apply, otherwise it will throw an exception. operator delete finally frees up space through free.
Locating new: positioning new expression is to initialize an object in the allocated original memory space by calling the constructor.

void main()
{
	int ptr[10];
	new(ptr,2) int(1);  //d positioning new does not open up a space and writes nothing. It defaults to the first space
}

**Difference between malloc/free and new/delete * *:

malloc/free and new/delete The common point is that they all apply for space from the heap and need to be released manually by the user.
The differences are:
 1. malloc and free Is a function, new and delete Is operator
 2. malloc The requested space will not be initialized, new Can initialize
 3. malloc When applying for space, you need to manually calculate the space size and transfer it, new Just follow the type of space
 4. malloc The return value of is void*, When in use, it must be forced to turn, new No, because new Followed by the type of space
 5. malloc When the application for space fails, the returned value is NULL´╝îTherefore, it must be judged empty when using, new No, but new Need to catch exception
 6. When applying for custom type objects, malloc/free It will only open up space, and will not call constructors and destructors new After applying for space, the constructor will be called to complete the initialization of the object, delete Before releasing the space, the destructor will be called to clean up the resources in the space

Tags: C++ Operation & Maintenance server

Posted on Tue, 23 Nov 2021 23:33:20 -0500 by sentback