[C++ basic knowledge] C\C++ memory management


First read the code and think about the problem:

int globalVar = 1;
static int staticGlobalVar = 1;
void Test()
{
    
    
 static int staticVar = 1;
 int localVar = 1;
 
 int num1[10] = {
    
    1, 2, 3, 4};
 char char2[] = "abcd";
 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);
}

problem:

  1. Multiple choice questions:
    Options: A. Stack B. Heap C. Data segment D.
    Where is the code segment 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) = ____;

answer:

1 、 CCCAA
ADADAB 2、40
5 4 4 4 4

Dynamic memory management in C language

malloc

void* malloc (size_t size);

This function applies for a continuous available space from the memory and returns a pointer to this space .
​ · If the development is successful, a pointer to the opened space is returned.
​ · If the development fails, a NULL pointer is returned, so the return value of malloc must be checked.
​ ·The type of the return value is void*, so the malloc function does not know the type of space opened up, and the user decides it when it is used.
​ · If the parameter size is 0, the behavior of malloc is undefined by the standard, depending on the compiler.

free

void free (void* ptr);

The free function is specifically used in the C language for the release and recovery of dynamic memory :
If the space pointed to by the parameter ptr is not dynamically opened, the behavior of the free function is undefined.
​ ·If the parameter ptr is a NULL pointer, the function does nothing.

calloc

void* calloc (size_t num,size_t size);

The function of the calloc function is:
​ ·Open up memory space for num data of size, and initialize each byte of the space to 0.
​ ·The difference with malloc is that calloc will apply for each of the memory space The byte is initialized to 0.

realloc

void* realloc (void * ptr,size_t size );

The emergence of realloc makes dynamic memory management more flexible. Realloc can adjust the size of dynamically opened memory:
·ptr is the memory address to be adjusted.
​ ·size is the new size after adjustment.
​ ·The return value points to the memory starting position after adjustment.
​ · On the basis of adjusting the memory size, realloc will also move the data in the original memory to the new space.
​ ·Realloc adjusts the memory space in two situations:
​ ·Case 1: There is enough space after the original space, and the memory to be expanded is directly added after the original memory, and the data in the original space does not change;
​ ·Case 2: When there is not enough space after the original space: Find another contiguous memory space of suitable size on the heap space to use. This function will return a new address.

Dynamic memory management methods new and delete in C++

Read the code:

void Test()
{
    
    
 // 申请单个Test类型的对象
 Test* p1 = new Test;
 delete p1;
 
 // 申请10个Test类型的对象
 Test* p2 = new Test[10];
 delete[] p2; }
 
 //注意:在申请自定义类型的空间时,new会调用构造函数
 //delete会调用析构函数,而malloc与free不会。

Built-in type

If you are applying for a built-in type of space, new and malloc, delete and free are basically similar, the difference is: new/delete applies for and releases the space of a single element, and new[] and delete[] apply for continuous space . And new will throw an exception when it fails to apply for space, and malloc will return NULL.

Custom type

Principle of new

  1. Call operator new function to apply for space
  2. Execute the constructor on the requested space to complete the object's construction and
    delete the principle
  3. Execute the destructor on the space to complete the cleanup of resources in the object
  4. The principle of calling the operator delete function to release the space of the object new T[N]
  5. Call operator new[] function, actually call operator new function in operator new[] to complete the application of N object spaces

  6. The principle of executing the constructor delete[] N times on the requested space
  7. Execute N destructors on the released object space to complete the cleanup of resources in N objects
  8. Call operator delete[] to release space, actually call operator delete in operator delete[] to release space

The difference between malloc/free and new/delete

The common point of malloc/free and new/delete is that they both apply for space from the heap and need to be released manually by the user. The differences are:

  1. malloc and free are functions , new and delete are operators
  2. The space requested by malloc will not be initialized, but new can be initialized
  3. When malloc space applications, manual calculations and passed space, new new space to keep only thereafter the type to
  4. The return value of malloc is void*, which must be forced when used. New is not required, because new is followed by the type of space
  5. When malloc fails to apply for space, it returns NULL, so it must be null when used, new does not need, but new needs to catch exceptions
  6. When applying for a custom type object, malloc/free will only open up space and will not call the constructor and destructor. New will call the constructor to complete the initialization of the object after applying for the space, and delete will call the destructor before releasing the space. Complete the cleanup of resources in the space

Guess you like

Origin blog.csdn.net/weixin_43962381/article/details/114987057