当前位置:网站首页>delete和delete[]引发的问题
delete和delete[]引发的问题
2022-07-01 09:12:00 【代码整洁之道】
一、问题1:delete 一个指向array new的指针,导致程序异常退出
Foo* p1 = new Foo[2];
delete p1;

1、arary new的动态申请的内存结构大致如下图(vs2022)
2、通过查看内存布局。
3、在operator delete打断点,发现传进来的指针指向第一个对象的地址,在free函数中导致崩溃。
对比调用delete [] p1,传进来的指针phead是指向记录array size大小的地址。
二、问题2:array delete 一个指向new的指针,导致析构函数不断调用
Foo* p2 = new Foo;
delete [] p2;

1、new的动态申请的内存结构大致如下图(vs2022)
2、通过查看内存布局。
3、在Foo的析构函数打断点,反汇编,发现控制调用析构函数的count是个随机值,导致析构函数不断被调用
三、测试代码
// -------- xxx.hpp
#pragma once
#include <iostream>
#include <memory>
class Foo {
public:
Foo() {
std::cout << " Foo : " << this << std::endl; }
~Foo() {
std::cout << "~Foo : " << this << std::endl; }
static void* operator new(size_t size);
static void operator delete(void* phead, size_t size);
static void* operator new[](size_t size);
static void operator delete[](void* pheda, size_t size);
private:
char _char1 = 170; // 0xAA
int _int1 = 2864434397; // 0XAABBCCDD
};
// operator new
void* Foo::operator new(size_t size)
{
void* ret = ::operator new(size);;
std::cout << "new, pointer : " << ret << ", size : " << size << std::endl;
return ret;
}
// operator delete
void Foo::operator delete(void* phead, size_t size)
{
std::cout << "delete, pointer : " << phead << ", size : " << size << std::endl;
free(phead);
}
// operator new[]
void* Foo::operator new[](size_t size)
{
void* ret = ::operator new[](size);
std::cout << "new [], pointer : " << ret << ", size : " << size << std::endl;
return ret;
}
// operator delete[]
void Foo::operator delete[](void* phead, size_t size)
{
std::cout << "delete [], pointer : " << phead << ", size : " << size << std::endl;
free(phead);
void test()
{
Foo* p1 = new Foo[2];
delete p1; // 异常退出
//delete [] p1;
Foo* p2 = new Foo;
delete[] p2; // 一直调用析构函数无数遍
//delete p2;
}
边栏推荐
- 【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + MQ系列 + NodeJs本地服务 + MySql存储
- [interview brush 101] linked list
- nacos簡易實現負載均衡
- Is it safe to dig up money and make new shares
- 2.4 activation function
- 猿人学第20题(题目会不定时更新)
- Daily office consumables management solution
- Implementation and application of queue
- 如何高效拉齐团队认知
- Shell脚本-变量的定义、赋值和删除
猜你喜欢

MySQL optimization

Daily practice of C language - day 80: currency change

樹結構---二叉樹2非遞歸遍曆

Phishing identification app

Pain points and solutions of equipment management in large factories

Personal decoration notes

Pain points and solutions of fixed assets management of group companies

Principle and application of single chip microcomputer timer, serial communication and interrupt system

TV size and viewing distance

FAQ | FAQ for building applications for large screen devices
随机推荐
JCL and slf4j
nacos簡易實現負載均衡
How to manage fixed assets efficiently in one stop?
LogBack
Shell脚本-特殊变量:Shell $#、$*、[email protected]、$?、$$
Shell script - special variables: shell $, $*, [email protected], $$$
2.2 【pytorch】torchvision.transforms
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DHT11 +nodejs local service + MySQL database
Understand shallow replication and deep replication through code examples
Class loading
Full mark standard for sports items in the high school entrance examination (Shenzhen, Anhui and Hubei)
nacos简易实现负载均衡
【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
【检测技术课案】简易数显电子秤的设计与制作
JCL 和 SLF4J
Mise en œuvre simple de l'équilibrage de la charge par nacos
Promise asynchronous programming
Why is the Ltd independent station a Web3.0 website!
Summary of reptile knowledge points
序列化、监听、自定义注解