delete() and free() in C++
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<stdio.h>
2 #include<stdlib.h>
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.
转载请注明:http://www.cnblogs.com/iloveyouforever/
2013-11-26 10:00:24
delete() and free() in C++的更多相关文章
- 如何区别数据库删除语句drop与delete与truncate?
1.delete:删除数据表中的行(可以删除某一行,也可以在不删除数据表的情况下删除所有行) 删除某一行:delete from 数据表名称 where 列名称=值: 删除所有行:delete*fro ...
- 数据库设计中的Soft Delete模式
最近几天有点忙,所以我们今天来一篇短的,简单地介绍一下数据库设计中的一种模式——Soft Delete. 可以说,该模式毁誉参半,甚至有非常多的人认为该模式是一个Anti-Pattern.因此在本篇文 ...
- 关于JavaScript中的delete操作
关于JavaScript中的delete操作 看到一道题,是这样的: (function(x){ delete x; return x; })(1); 1 null undefined Error 我 ...
- Git异常:Cannot delete the branch 'test1' which you are currently on
GitHub实战系列汇总:http://www.cnblogs.com/dunitian/p/5038719.html ———————————————————————————————————————— ...
- HTTP Method详细解读(`GET` `HEAD` `POST` `OPTIONS` `PUT` `DELETE` `TRACE` `CONNECT`)
前言 HTTP Method的历史: HTTP 0.9 这个版本只有GET方法 HTTP 1.0 这个版本有GET HEAD POST这三个方法 HTTP 1.1 这个版本是当前版本,包含GET HE ...
- IIS7.5上的REST服务的Put,Delete操作发生HTTP Error 405.0 - Method Not Allowed 解决方法
WebDAV 是超文本传输协议 (HTTP) 的一组扩展,为 Internet 上计算机之间的编辑和文件管理提供了标准.利用这个协议用户可以通过Web进行远程的基本文件操作,如拷贝.移动.删除等.在I ...
- ASP.NET Core 中文文档 第二章 指南(4.10)检查自动生成的Detail方法和Delete方法
原文 Examining the Details and Delete methods 作者 Rick Anderson 翻译 谢炀(Kiler) 校对 许登洋(Seay).姚阿勇(Mr.Yao) 打 ...
- new/delete重载
在c++中,有时我们需要在运行阶段为一个变量分配未命名的内存,并使用指针来访问它,这里就可以用到new关键字.另外需要指出的是,new分配的内存块通常与常规变量分配的内存块不同,常规变量的值都储存在被 ...
- EC笔记:第三部分:16成对使用new和delete
我们都知道,申请的资源,使用完毕后要释放.但是这个释放动作,一定要注意. 举个例子,很多人动态分配的资源,在使用之后,往往直接调用了delete,而不管申请资源的时候用的是new还是new[]. 如下 ...
- Spring boot: Request method 'DELETE' not supported, Request method 'PUT' not supported, Request method 'POST' not supported
GET,POST,PUT,DELETE, Spring都支持,不要怀疑Spring, 一定是前端发送的rest 请求和后端的响应不匹配, 查找原因以及解决办法, 很简单 用chrome打开F12控制台 ...
随机推荐
- ESP32-IDF安装并在VSCode上编译Hello World
ESP32-IDF安装 准备工作 安装python 3 安装方法参考链接:https://blog.csdn.net/hg_qry/article/details/106415252 安装git 安装 ...
- [LINUX] Arch Linux 硬盘拷贝式装系统+新增 home 分区
目录 前言 1. 实操 1.1 整个磁盘拷贝 1.2 创建 home 分区 1.3 修改 fstab 实现自动挂载 2. 涉及到的知识点 2.1 fstab 2.2 dd 命令 2.3 fdisk 命 ...
- 第五周PTA笔记 后缀表达式+后缀表达式计算
后缀表达式 所谓后缀表达式是指这样的一个表达式:式中不再引用括号,运算符号放在两个运算对象之后,所有计算按运算符号出现的顺序,严格地由左而右进行(不用考虑运算符的优先级). 如:中缀表达式 3(5–2 ...
- 1组-Alpha冲刺-1/6
一.基本情况 队名:震震带着六菜鸟 组长博客:https://www.cnblogs.com/Klein-Wang/p/15526531.html 小组人数:7人 二.冲刺概况汇报 王业震 过去两天完 ...
- [hdu6974]Destinations
注意到一个人的三条链一定不会同时选(忽略仅选一个终点的限制),因为其有公共点(起点) 换言之,问题相当于给定$3m$条链,选择$m$条没有公共点的链,并最小化代价和 进一步的,显然也不存在多于$m$条 ...
- [bzoj5417]你的名字
先考虑l=1,r=|s|的部分分,需要求出t每一个前缀的不是s子串的最长后缀,记作pp[k],有以下限制:1.pp[pos[k]]<len(pos[k]表示k的某一个结束位置),因为不能被匹配 ...
- [luogu5438]记忆
令$f(x)=\frac{x}{\max_{k^{2}|x}k^{2}}$,最优解即将$f(l),f(l+1),...,f(r)$排序,那么每存在一种不同的数则答案减1,那么$x$出现当且仅当$f(x ...
- LRU缓存
LRU缓存 struct Node{ int key; int value; Node* next; Node* pre; Node(): key(-1), value(-1), next(nullp ...
- c++基础知识02
1.前置与后置区别 #include<iostream> using namespace std; int main() { //前置和后置区别 //前置递增或递减 先让变量加减1 然后进 ...
- 洛谷 P7154 - [USACO20DEC] Sleeping Cows P(dp)
Portal 题意: 给出两个序列 \(a_1,a_2,\dots,a_n\),\(b_1,b_2,\dots,b_n\),\(i\) 与 \(j\) 能匹配当且仅当 \(a_i\leq b_j\). ...