[C++]: Memory management: C++ memory distribution || Dynamic memory management in C++ (new || delete)

1. C/C++ memory distribution


【illustrate】
1. The stack is also called a stack – non-static local variables/function parameters/return values, etc. The stack grows downwards
2. Memory mapping segment is an efficient I/O mapping method, used to load a shared dynamic memory library. Users can use the system interface to create shared shared memory for inter-process communication. (If you haven’t learned this in the Linux course, you just need to understand it now)
3. The heap is used for dynamic memory allocation when the program is running, and the heap can grow.
4. Data segment – stores global data and static data.
5. Code segment – executable code/read-only constants
Let’s first look at the following piece of code and related questions

int globalVar = 1;
static int staticGlobalVar = 1;
voidTest()
{<!-- -->
static int staticVar = 1;
int localVar = 1;
int num1[10] = {<!-- --> 1, 2, 3, 4 };
char char2[] = "abcd";
const char* pChar3 = "abcd";
int* ptr1 = (int*)malloc(sizeof(int) * 4);
int* ptr2 = (int*)calloc(4, sizeof(int));
int* ptr3 = (int*)realloc(ptr2, sizeof(int) * 4);
free(ptr1);
free(ptr3);
}

1. Multiple choice questions:
Options: A. Stack B. Heap C. Data segment (static area) D. Code segment (constant area)
Where is globalVar? ____ Where is staticGlobalVar? ____
Where is staticVar? ____ Where is localVar? ____
Where is num1? ____
Where is char2? ____ Where is *char2? ___
Where is pChar3? ___ Where is *pChar3? ____
Where is ptr1? ____ Where is *ptr1? ____
2. Fill in the blanks:
sizeof(num1) = ____
sizeof(char2) = ____ strlen(char2) = ____
sizeof(pChar3) = ____ strlen(pChar3) = ____
sizeof(ptr1) = ____
3. What is the difference between sizeof and strlen?


2. Dynamic memory management method in C language: malloc/calloc/realloc/free

void Test ()
{<!-- -->
int* p1 = (int*) malloc(sizeof(int));
free(p1);
// 1. What is the difference between malloc/calloc/realloc?
int* p2 = (int*)calloc(4, sizeof (int));
int* p3 = (int*)realloc(p2, sizeof(int)*10);
// Is free(p2) needed here?
free(p3);
}

[Interview questions]
1. What is the difference between malloc/calloc/realloc?
2. The implementation principle of malloc?
Implementation principle of malloc in glibc
Implementation principle of malloc in glibc

3. C++ memory management method

The C language memory management method can continue to be used in C++, but it is ineffective in some places and is more troublesome to use. Therefore, C++ has proposed its own memory management method: dynamic memory management through new and delete operators.
new/delete operation built-in types

void Test()
{<!-- -->
// Dynamically apply for a space of type int
int* ptr4 = new int;
// Dynamically apply for a space of type int and initialize it to 10
int* ptr5 = new int(10);
// Dynamically apply for 10 int type spaces
int* ptr6 = new int[3];
delete ptr4;
delete ptr5;
delete[] ptr6;
}


Note: To apply for and release space for a single element, use the new and delete operators. To apply for and release continuous space, use new[] and delete[]. Note: Use when matching.

int main()
{<!-- -->
int* p2 = (int*)malloc(sizeof(int));
// Automatically calculate size, no need to force transfer
int* p3 = new int;

int* p4 = (int*)malloc(sizeof(int)*10);
int* p5 = new int[10];

free(p2);
free(p4);

delete p3;
delete[] p5;

// Additional support for opening space + initialization
int* p6 = new int(10);
int* p7 = new int[10]{<!-- -->1,2,3};
int* p8 = new int[10]{<!-- -->};

return 0;
}int main()
{<!-- -->
int* p2 = (int*)malloc(sizeof(int));
// Automatically calculate size, no need to force transfer
int* p3 = new int;

int* p4 = (int*)malloc(sizeof(int)*10);
int* p5 = new int[10];

free(p2);
free(p4);

delete p3;
delete[] p5;

// Additional support for opening space + initialization
int* p6 = new int(10);
int* p7 = new int[10]{<!-- -->1,2,3};
int* p8 = new int[10]{<!-- -->};

return 0;
}int main()
{<!-- -->
int* p2 = (int*)malloc(sizeof(int));
// Automatically calculate size, no need to force transfer
int* p3 = new int;

int* p4 = (int*)malloc(sizeof(int)*10);
int* p5 = new int[10];

free(p2);
free(p4);

delete p3;
delete[] p5;

// Additional support for opening space + initialization
int* p6 = new int(10);
int* p7 = new int[10]{<!-- -->1,2,3};
int* p8 = new int[10]{<!-- -->};

return 0;
}

class A
{<!-- -->
public:
A(int a = 0)
: _a(a)
{<!-- -->
cout << "A():" <<this << endl;
}

~A()
{<!-- -->
cout << "~A():" <<this << endl;
}

private:
int _a;
};

struct ListNode
{<!-- -->
ListNode* _next;
int _val;

ListNode(int val = 0)
:_val(0)
, _next(nullptr)
{<!-- -->}
};

int main()
{<!-- -->
Malloc has no way to support the initialization of dynamically requested custom objects.
A* p1 = (A*)malloc(sizeof(A));
p1->_a = 0;
p1->A(1);

Custom type, open space + call constructor initialization
A* p2 = new A;
A* p3 = new A(3);

Custom type, call destructor + release space
delete p2;
delete p3;

A* p4 = new A[10];
delete[] p4;

A aa1(1);
A aa2(2);
A* p5 = new A[10]{<!-- --> aa1, aa2 };
delete[] p5;


A* p6 = new A[10]{<!-- --> A(1), A(2) };
delete[] p6;

A* p7 = new A[10]{<!-- --> 1, 2 };
delete[] p7;

ListNode* n1 = new ListNode(1);
ListNode* n2 = new ListNode(2);
ListNode* n3 = new ListNode(3);
ListNode* n4 = new ListNode(4);
ListNode* n5 = new ListNode(5);

n1->_next = n2;
n2->_next = n3;
n3->_next = n4;
n4->_next = n5;
return 0;
}

New and delete operation custom types

class A
{<!-- -->
public:
A(int a = 0)
: _a(a)
{<!-- -->
cout << "A():" <<this << endl;
}
~A()
{<!-- -->
cout << "~A():" <<this << endl;
}
private:
int _a;
};
int main()
{<!-- -->
// The biggest difference between new/delete and malloc/free is that new/delete does not only open space for [custom types]
Constructors and destructors are also called
A* p1 = (A*)malloc(sizeof(A));
A* p2 = new A(1);
free(p1);
delete p2;
// Built-in types are almost the same
int* p3 = (int*)malloc(sizeof(int)); // C
int* p4 = new int;
free(p3);
delete p4;
A* p5 = (A*)malloc(sizeof(A)*10);
A* p6 = new A[10];
free(p5);
delete[] p6;
return 0;

Note: When applying for a custom type of space, new will call the constructor, delete will call the destructor, but malloc and free will not.