当前位置:网站首页>用New,delete和用malloc,free申请,释放堆区空间
用New,delete和用malloc,free申请,释放堆区空间
2022-07-26 22:37:00 【Sy++】
C语言中的malloc,free函数
malloc函数
extern void *malloc(unsigned int num_bytes);返回:void类型的指针
功能:向系统申请分配长度为num_bytes长度的内存块,分配成功则返回一个指向该内存块的指针,分配失败则返回空指针NULL
缺点:
1.其返回的指针不是指定类型的指针,需要进行强制转换
Maker* m = (Maker*)malloc(sizeof(Maker));2.在C++里,不会调用类的构造函数,同理free也不会调用类的析构函数
class Maker
{
public:
Maker()
{
cout << "构造函数" << endl;
}
Maker(int a)
{
cout << "有参构造函数" << endl;
}
~Maker()
{
cout << "析构函数" << endl;
}
};
void test01()
{
//用c语言申请堆区空间,不会调用构造函数和析构函数
Maker* m = (Maker*)malloc(sizeof(Maker));
//对象释放时不会调用析构函数
free(m);
}free函数
void free(void *FirstByte)功能:将malloc分配的内存释放掉,通常与malloc一起使用
缺点:在C++里不会调用类的析构函数
注意:free函数释放的是内存,而不是指针,当你释放完内存后,指针指向的是一片未知的内存,所以你需要将指针重置为NULL
C++语言中的new,delete函数
new函数
功能:向堆区申请一片内存空间,成功则返回这片空间的首地址,失败则返回NULL,并且其会自动转换成指定的数据类型指针
用法:
类的用法:
Maker* m = new Maker(2);
new用于数组:
1.基础类型数组
int* pInt = new int[10];
char* pChar = new char[64];
2.对象数组
Maker* m = new Maker[2];//只能调用的是无参构造注意:
1.在创建对象数组的时候只能调用无参构造,不能调用有参构造,不过在有些编译器上是可以调用有参构造的,形式如下,不过大部分编译器是调用不了的
Maker* m2 = new Maker[2]{ Maker(10),Maker(20) };2.new函数不要与free函数混用,因为,new函数会调用构造函数,而free函数不会调用析构函数
3.不要用空指针来接new的对象,delete时不会调用析构函数
void test03()
{
void* m = new Maker;
//如果用void*来接new的对象,那么delete时不会调用析构函数
delete m;
//在编译阶段,那么编译器就确定好了函数的调用地址,
//C++编译器不认识void*,不知道void*指向哪个函数,所以不会调用析构汉化
//这种编译方式叫做静态联编
}优点: new函数会调用类的构造函数,并不需要类型转换。
delete函数
功能:将new分配的内存释放掉,通常与malloc一起使用
用法:
当new的对象不是数组时:
delete 对象名;
当new的对象是数组时:
delete[] 对象名;注意:
1.delete函数释放的也是内存,而不是指针,所以需要在释放完内存后,将指针置位空
delete m;
m=NULL;优点:delete函数会调用类的析构函数
边栏推荐
- The crawler parses the object of the web page. Element name method
- LeetCode题目——数组篇
- 信号与系统冲激响应与阶跃响应
- 2022-07-17:1, 2, 3... N-1, N, n+1, n+2... In this sequence, only one number has repetition (n). This sequence is unordered. Find the repeated number n. This sequence is ordered. Find the repeated numb
- Viterbi Viterbi decoding bit error rate simulation, modulation is QPSK, channel is Gaussian white noise
- When the label begins with "IMS", why does logcat not print the log?
- 傅里叶分析(基础介绍)
- Several search terms
- AutoCAD的卸载后重新安装,删除注册表的详细过程
- TypeScript(tsconfig.json)
猜你喜欢

Three tier architecture simulation

Today's 20220719 toss deeplobcut

蒙着头配置deeplabcut 1

Practice of data storage scheme in distributed system

Shufflenet series (2): explanation of shufflenet V2 theory

Recbole use 1

13_集成学习和随机森林(Ensemble Learning and Random Forests)

信号与系统冲激响应与阶跃响应

Complete review of parsing web pages

uni-app学习(二)
随机推荐
When the label begins with "IMS", why does logcat not print the log?
6_梯度下降法(Gradient Descent)
Course notes of Professor Dalin of robotics platform
Deep learning of parameter adjustment skills
AutoCAD的卸载后重新安装,删除注册表的详细过程
"Syntaxerror: future feature annotations is not defined"
类与对象笔记一
C and pointer Chapter 18 runtime environment 18.2 interface between C and assembly language
AlexNet(Pytorch实现)
Midge paper reading notes
UNET notes
Request attribute in crawler
Helicopter control system based on Simulink
放图仓库-3(功能图像)
RecBole使用1
知识蒸馏——pytorch实现
Uni app learning (II)
94. Middle order traversal of binary tree
LeetCode题目——二叉树篇
Pyautogui usage example