delete() and free() in C++
2013-11-26 10:01
141 查看
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.
#include<stdio.h> #include<stdlib.h> int main() { int x; int *ptr1 = &x; int *ptr2 = (int *)malloc(sizeof(int)); int *ptr3 = new int; int *ptr4 = NULL; /* delete Should NOT be used like below because x is allocated on stack frame */ delete ptr1; /* delete Should NOT be used like below because x is allocated using malloc() */ delete ptr2; /* Correct uses of delete */ delete ptr3; delete ptr4; getchar(); return 0; }
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
转载请注明:http://www.cnblogs.com/iloveyouforever/
2013-11-26 10:00:24
相关文章推荐
- "delete this" in C++
- Something More about "new" &"delete" in C++
- vc++学习笔记new和delete
- delete expired backup 和 delete obsolete
- Linux delete define files in correct dir and sub-dir
- SQLiteDatabase类的query(),insert(),update(),delete()方法参数说明
- 通过重载new和delete实现简单的对象池
- 【leetcode】Insert Delete GetRandom O(1)
- Delete Roaming Profile Cache
- Error:Execution failed for task ':clean'. > Unable to delete directory :\build\intermediates (转)
- SQL truncate 、delete与drop区别
- 关于大量文件的copy,delete操作方式效率初探
- 【OpenStack】Nova中的delete和restore
- new/delete与malloc/free的区别
- 指针和动态数组 new ,delete
- c 里面的free和c++的delete的区别
- (转载)警惕“delete void*”
- springmvc实现REST中的GET、POST、PUT和DELETE
- delete运算符
- PIQ18: Delete middle node of a linked list