Detailed explanation and application of malloc and free new and delete in C++
- 2020-06-12 10:14:07
- OfStack
C++ Interviews often ask questions about the differences between malloc/free and new/delete. There are different versions of the explanations available on the Web.
The similarities
1. You can apply for dynamic heap memory.Differences between the two
new/delete is the operator of C++, and malloc/free is the standard library function of C/C++. 2. The object applied by new can be understood as an object. The constructor will be called when new returns a pointer to the object, and the destructor will be called when delete; malloc only requests memory, not objects. 3.new/delete is reserved word, no need for header file support; malloc/free requires header file library function support.Matters needing attention
1. The memory applied with new must be freed with delete. 2. The memory requested with new[] must be freed with delete[]. 3. After delete frees the memory, the pointer value remains unchanged. A good style is to set the pointer to NULL after freeing, for example,delete p; p = NULL. 4. Memory requested with malloc must be freed with free.use
#include "stdafx.h"
#include <stdio.h>
#include "stdlib.h"
#include <string.h>
struct Stu
{
char name[32];
int age;
};
int main()
{
/**************************** Basic usage **********************************/
// To apply for 1 a int type
int *p1 = new int; // Apply for assignment directly int* p1 = new int(3);
int *p2 = (int*)malloc(sizeof(int));
// To apply for 1 a char type
char *p3 = new char; // Apply for assignment directly char *p3 = new char('c');
char *p4 = (char*)malloc(sizeof(char));
// To apply for 1 a int type 1 Dimensional array
int *p5 = new int[5]; // Apply for assignment directly int *p5 = new int[5]{1,2,3,4,5};
int *p6 = (int*)malloc(sizeof(int)*5);
// To apply for 1 a char type 1 Dimensional array
char* p7 = new char[6]; // Apply for assignment directly char* p7 = new char[3]{'a', 'v', 'c'};
char* p8 = (char*)malloc(sizeof(char)*6);
// To apply for 1 a int type 2 Dimensional array
int(*p9)[2] = new int[2][2]; // Apply for assignment directly int(*p9)[2] = new int[2][2]{ 1,2,3,4 };
int(*p10)[2] = (int(*)[2])malloc(sizeof(int)*2*2);
// To apply for 1 a char type 2 Dimensional array
char(*p11)[2] = new char[2][2];
char(*p12)[2] = (char(*)[2])malloc(sizeof(char)*2*2);
/***************************** To apply for 2 Level pointer memory **********************************/
// To apply for 2 Class pointer (new,delete)
char** p13 = new char*[2];
p13[0] = "aaaaaaaaaa";
p13[1] = "vvvvvvvvvv";
delete p13;
// To apply for 2 Class pointer (malloc, free)
char** p14 = (char**)malloc(sizeof(char*)*2);
p14[0] = "cccccccc";
p14[1] = "dddddddd";
delete p14;
/****************************** Apply for structural in vivo storage *********************************/
//new delete
Stu* pStu1 = new Stu;
Stu* pStu2 = new Stu{"wpf", 10};
Stu* pStu3 = new Stu[1024];
delete pStu1;
delete pStu2;
delete[] pStu3;
//malloc free
Stu* pStu4 = (Stu*)malloc(sizeof(Stu));
memset(pStu4, 0, sizeof(Stu));
free(pStu4);
getchar();
}
conclusion