为什么delete NULL是安全的,delete后发生了什么

已知的

  1. 必须与new成对出现,否则会导致内存泄漏;
  2. 不能连续delete
int* pint=new int(1);
delete pint;
delete pint;//报错

3.不能delete栈空间,换句话说,只能释放用户分配的堆空间。
##delete NULL是安全的

  1. C++规定,delete(void*)0; 是正确的
  2. 现在的编译器都会自带#define NULL 0的宏
  3. 所以,除非使用的编译器恰好不带有这样的宏,或者自行修改了NULL的宏定义,delete NULL都是安全的
    ##delete后发生了什么?
    在这里插入图片描述
    在第三步,执行operator delete后,pobj指向的堆空间被释放;
    也就是pobj不再指向原来的地址,但pobj的值也不是NULL,而是会指向一个随机地址。换句话说,pobj会变成一个野指针;
    这也解释了为什么我们不能连续delete同一指针。
int main() {

	int* pobj = new int(1);
	cout << "pobj的地址:" << &pobj 
		<< "	pobj指向的地址:" << pobj << endl;
	delete pobj;
	cout << "pobj的地址:" << &pobj 
		<< "	pobj指向的地址:" << pobj << endl;

	return 0;
}

在这里插入图片描述

delete的正确操作

if(pobj){//毕竟可能有编译器没有上述的宏,这样总归是会更安全的
	delete pobj;
	pobj=NULL;//避免出现野指针,同时以后即使再次对pobj进行了这样的一套delete操作,也是安全的
}

参考了以下文章,感谢大佬们通俗易懂的解释:
@千么漾漾
@QWDMchengzi
本文仅作本人复习巩固用,能力有限,如有纰漏,烦请指出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.qihoo360.mobilesafe.provider; import android.content.ContentProvider; import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteQueryBuilder; import android.net.Uri; import android.os.ParcelFileDescriptor; import android.text.TextUtils; import bl; import c; import d; import e; import f; import g; import h; import i; import j; import java.io.File; import java.util.HashMap; import java.util.List; import java.util.Map; import k; import l; import m; import n; import o; import p; import q; import r; import s; import t; public class SafeGuardProvider extends ContentProvider { public static bl a; private static HashMap b; private static HashMap c; private static HashMap d; private static HashMap e; private static HashMap f; private static HashMap g; private static HashMap h; private static HashMap i; private static HashMap j; private static HashMap k; private static HashMap l; private static HashMap m; private static HashMap n; private static HashMap o; private static HashMap p; private static HashMap q; private static HashMap r; private static HashMap s; private static final UriMatcher t; static { UriMatcher localUriMatcher = new UriMatcher(-1); t = localUriMatcher; localUriMatcher.addURI("com.qihoo360.mobilesafeguard", "blacklist", 1); t.addURI("com.qihoo360.mobilesafeguard", "blacklist/#", 2); t.addURI("com.qihoo360.mobilesafeguard", "msg_history", 3); t.addURI("com.qihoo360.mobilesafeguard", "msg_history/#", 4); t.addURI("com.qihoo360.mobilesafeguard", "call_history", 5); t.addURI("com.qihoo360.mobilesafeguard", "call_history/#", 6); t.addURI("com.qihoo360.mobilesafeguard", "whitelist", 7); t.addURI("com.qihoo360.mobilesafeguard", "whitelist/#", 8); t.addURI("com.qihoo360.mobilesafeguard", "private_in_call", 9); t.addURI("com.qihoo360.mobilesafeguard", "private_in_call/#", 10); t.addURI("com.qihoo360.mobilesafeguard", "private_out_call", 11); t.addURI("com.qihoo360.mobilesafeguard", "private_out_call/#", 12); t.addURI("com.qihoo360.mobilesafeguard", "private_in_mms", 13); t.addURI("com.qihoo360.mobilesafeguard", "private_in_mms/#", 14); t.addURI("com.qihoo360.mobilesafeguard", "private_out_mms", 15); t.addURI("com.qihoo360.mobilesafeguard", "private_out_mms/#", 16); t.addURI("com.qihoo360.mobilesafeguard", "private_mms", 19); t.addURI("com.qihoo360.mobilesafeguard", "private_mms/#", 20); t.addURI("com.qihoo360.mobilesafeguard", "privatelist", 17); t.addURI("com.qihoo360.mobilesafeguard", "privatelist/#", 18); t.addURI("com.qihoo360.mobilesafeguard", "smartwhite", 21); t.addURI("com.qihoo360.mobilesafeguard", "smartwhite/#", 22); t.addURI("com.qihoo360.mobilesafeguard", "private_call_in", 23); t.addURI("com.qihoo360.mobilesafeguard", "private_call_in/#", 24); t.addURI("com.qihoo360.mobilesafeguard", "private_call_out", 25); t.addURI("com.qihoo360.mobilesafeguard", "private_call_out/#", 26); t.addURI("com.qihoo360.mobilesafeguard", "private_mms_in", 27); t.addURI("com.qihoo360.mobilesafeguard", "private_mms_in/#", 28); t.addURI("com.qihoo360.mobilesafeguard", "private_mms_out", 29); t.addURI("com.qihoo360.mobilesafeguard", "private_mms_out/#", 30); t.addURI("com.qihoo360.mobilesafeguard", "private_message", 33); t.addURI("com.qihoo360.mobilesafeguard", "private_message/#", 34); t.addURI("com.qihoo360.mobilesafeguard", "privatecontacts", 31); t.addURI("com.qihoo360.mobilesafeguard", "privatecontacts/#", 32); t.addURI("com.qihoo360.mobilesafeguard", "ipnouselist", 35); t.addURI("com.qihoo360.mobilesafeguard", "ipnouselist/#", 36); HashMap localHashMap1 = new HashMap(); b = localHashMap1; localHashMap1.put("_id", "_id"); b.put("contact_name", "contact_name"); b.put("phone_number", "phone_number"); b.put("blocked_type", "blocked_type"); HashMap localHashMap2 = new HashMap(); c = localHashMap2; localHashMap2.put("_id", "_id"); c.put("address", "address"); c.put("date", "date"); c.put("subject", "subject"); c.put("body", "body"); c.put("read", "read"); c.put("type", "type"); HashMap localHashMap3 = new HashMap(); d = localHashMap3; localHashMap3.put("_id", "_id"); d.put("address", "address"); d.put("date", "date"); d.put("read", "read"); d.put("block_type", "block_type"); HashMap localHashMap4 = new HashMap(); e = localHashMap4; localHashMap4.put("_id", "_id"); e.put("contact_name", "contact_name"); e.put("phone_number", "phone_number"); HashMap localHashMap5 = new HashMap(); f = localHashMap5; localHashMap5.put("_id", "_id"); f.put("name", "name"); f.put("number", "number"); f.put("date", "date"); f.put("blocked_type", "blocked_type"); HashMap localHashMap6 = new HashMap(); g = localHashMap6; localHashMap6.put("_id", "_id"); g.put("name", "name"); g.put("number", "number"); g.put("date", "date"); HashMap localHashMap7 = new HashMap(); h = localHashMap7; localHashMap7.put("_id", "_id"); h.put("name", "name"); h.put("address", "address"); h.put("date", "date"); h.put("subject", "subject"); h.put("body", "body"); HashMap localHashMap8 = new HashMap(); i = localHashMap8; localHashMap8.put("_id", "_id"); i.put("name", "name"); i.put("address", "address"); i.put("date", "date"); i.put("subject", "subject"); i.put("body", "body"); HashMap localHashMap9 = new HashMap(); j = localHashMap9; localHashMap9.put("_id", "_id"); j.put("name", "name"); j.put("address", "address"); j.put("date", "date"); j.put("subject", "subject"); j.put("body", "body"); j.put("mms_recv_type", "mms_recv_type"); j.put("mms_type", "mms_type"); j.put("read", "read"); HashMap localHashMap10 = new HashMap(); k = localHashMap10; localHashMap10.put("_id", "_id"); k.put("contact_name", "contact_name"); k.put("phone_number", "phone_number"); k.put("blocked_type", "blocked_type"); HashMap localHashMap11 = new HashMap(); l = localHashMap11; localHashMap11.put("_id", "_id"); l.put("p_n", "p_n"); l.put("s_a_t", "s_a_t"); HashMap localHashMap12 = new HashMap(); m = localHashMap12; localHashMap12.put("_id", "_id"); m.put("name", "name"); m.put("pre_number", "pre_number"); m.put("number", "number"); m.put("date", "date"); m.put("blocked_type", "blocked_type"); HashMap localHashMap13 = new HashMap(); n = localHashMap13; localHashMap13.put("_id", "_id"); n.put("name", "name"); n.put("pre_number", "pre_number"); n.put("number", "number"); n.put("date", "date"); HashMap localHashMap14 = new HashMap(); o = localHashMap14; localHashMap14.put("_id", "_id"); o.put("name", "name"); o.put("pre_address", "pre_address"); o.put("address", "address"); o.put("date", "date"); o.put("subject", "subject"); o.put("body", "body"); HashMap localHashMap15 = new HashMap(); p = localHashMap15; localHashMap15.put("_id", "_id"); p.put("name", "name"); p.put("pre_address", "pre_address"); p.put("address", "address"); p.put("date", "date"); p.put("subject", "subject"); p.put("body", "body"); HashMap localHashMap16 = new HashMap(); q = localHashMap16; localHashMap16.put("_id", "_id"); q.put("name", "name"); q.put("pre_address", "pre_address"); q.put("address", "address"); q.put("date", "date"); q.put("subject", "subject"); q.put("body", "body"); q.put("mms_recv_type", "mms_recv_type"); q.put("mms_type", "mms_type"); q.put("read", "read"); HashMap localHashMap17 = new HashMap(); r = localHashMap17; localHashMap17.put("_id", "_id"); r.put("contact_name", "contact_name"); r.put("pre_number", "pre_number"); r.put("phone_number", "phone_number"); r.put("blocked_type", "blocked_type"); HashMap localHashMap18 = new HashMap(); s = localHashMap18; localHashMap18.put("_id", "_id"); s.put("contact_name", "contact_name"); s.put("phone_number", "phone_number"); } public int delete(Uri paramUri, String paramString, String[] paramArrayOfString) { Object localObject1 = " AND ("; String str1 = ""; ContentResolver localContentResolver = null; SQLiteDatabase localSQLiteDatabase; try { localSQLiteDatabase = a.getWritableDatabase(); localObject1 = t.match(paramUri); switch (localObject1) { default: String str2 = "Unknown URI " + paramUri; localObject1 = new IllegalArgumentException(str2); label225: throw ((Throwable)localObject1); case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 19: case 20: case 17: case 18: case 21: case 22: case 23: case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 33: case 34: case 31: case 32: case 35: case 36: } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值