Make writing a habit together! This is the 8th day of my participation in the “Gold Digging Day New Plan ยท April More Text Challenge”. Click here for more details.
Dynamic memory management in C language
๐ฆ malloc/calloc, realloc, and free
P2 calloc a space, P3 realloc P2, to P2 free? โ
void Test (a)
{
int* p1 = (int*) malloc(sizeof(int));
free(p1);
int* p2 = (int*)calloc(4.sizeof (int));
int* p3 = (int*)realloc(p2, sizeof(int) *10);
free(p3);
}
Copy the code
๐ instructions
This is where realLOc comes inSo it’s not good to write the above program:The difference between Malloc/Calloc /realloc
Malloc -> open space
Calloc is equivalent to malloc + memset(0) -> open space + initialization
Realloc can realize malloc effect when used alone (not initialize) – > open space | malloc/calloc spatial expansion
C++ memory management
C memory management can continue to be used in C++, but there are places where it is ineffective and cumbersome to use, so C++ has introduced its own approach to memory management: dynamic memory management through the new and DELETE operators.
๐ฆ new/delete Built-in type of the operation
int main(a)
{
/ / library functions
int* p1 = (int*)malloc(sizeof(int));
free(p1);
// Operator/keyword
int* p2 = new int;
delete p2;
return 0;
}
Copy the code
๐ instructions
What is the difference between malloc/free and new/delete โ
- If the dynamically applied object is of built-in type, then malloc and Free are no different
- There is a difference between malloc and free if the dynamically applied object is of a custom type
๐ฆ New and delete user-defined types
class A
{
public:
A(int a = 0/*int b = 0*/)
:_a(a)
{
cout << "A()" << endl;
}
~A()
{
cout << "~A()" << endl;
}
private:
int _a;
};
int main(a)
{
A* p3 = (A*)malloc(sizeof(A));
free(p3);
A* p4 = new A;
//A* p4 = new A(10);
//A* p4 = new A(10, 20);
delete p4;
/ / array
int* p5 = (int*)malloc(sizeof(int) * 10);
free(p5);
int* p6 = new int[10];
delete[]p6;
A* p7 = new A[10];// Call 10 constructs
delete[]p7;// Call destructor 10 times
return 0;
}
Copy the code
๐ instructions
- For the built-in types malloc/free only opens/frees space
- Custom types new/delete not only open/free space, but also call constructors and destructors — constructors can be called, and multiple arguments can be passed
Note that we don’t need the default constructor for class new A; However, the default constructor is required when new A[10] is used
Use new/delete as much as possible in C++, because if malloc/free can do it, so can new/delete; What new/delete can do, malloc/free may not.
Note that applying and freeing space for individual elements, using the new and delete operators, applying and freeing contiguous Spaces, using new[] and delete[]
What use is this feature โ
struct ListNode
{
int _val;
ListNode* _next;
ListNode(int val)
: _val(val)
, _next(nullptr) {}};int main(a)
{
//C
ListNode* n1 = (ListNode*)malloc(sizeof(ListNode));
n1->_val = 1;
n1->_next = nullptr;
//C++
ListNode* n2 = new ListNode(1);
return 0;
}
Copy the code
Niujiaojian problem โ
int* p1 = (int*)malloc(sizeof(int) * 10);
free(p1);
delete p1;
int* p2 = new int;
delete p2;
free(p2);
int* p2 = new int[10];
delete[]p2;
delete[10]p2;
delete p2;//errfree p2; /errCopy the code
๐ instructions
Keep to malloc โ free, new โ delete, new โ delete[] โ [], or else it may crash.