博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
delete() and free() in C++
阅读量:4600 次
发布时间:2019-06-09

本文共 1041 字,大约阅读时间需要 3 分钟。

 

  

  In C++, delete operator should only be used either for the pointers pointing to the memory allocated using new operator or for a NULL pointer, and free() should only be used either for the pointers pointing to the memory allocated using malloc() or for a NULL pointer.

1 #include
2 #include
3 int main() 4 { 5 int x; 6 int *ptr1 = &x; 7 int *ptr2 = (int *)malloc(sizeof(int)); 8 int *ptr3 = new int; 9 int *ptr4 = NULL;10 11 /* delete Should NOT be used like below because x is allocated on stack frame */13 delete ptr1; 14 15 /* delete Should NOT be used like below because x is allocated using malloc() */17 delete ptr2; 18 19 /* Correct uses of delete */20 delete ptr3;21 delete ptr4;22 23 getchar();24 return 0;25 }

 

 

 

  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

  转载请注明:

  2013-11-26  10:00:24

 

转载于:https://www.cnblogs.com/iloveyouforever/p/3442728.html

你可能感兴趣的文章
使用Maven构建多模块企业项目
查看>>
约瑟夫问题
查看>>
python变量传递给系统命令的方法
查看>>
JS常见的四种设计模式
查看>>
12.Hamming Distance(汉明距离)
查看>>
Java 数据流
查看>>
Vuex state 状态浅解
查看>>
添加图像
查看>>
maven 学习---Eclipse构建Maven项目
查看>>
dup和dup2重定向标准输出到文件
查看>>
[转]C#对Excel报表进行操作(读写和基本操作)
查看>>
第一次立会
查看>>
ActiveMQ使用教程
查看>>
图片的拷贝
查看>>
python函数2
查看>>
PHP 写文件的例子
查看>>
数据库的增删改查
查看>>
es6相关知识点
查看>>
php生成验证码 参考PHP手册
查看>>
[Hadoop]Hadoop章2 HDFS原理及读写过程
查看>>