当前位置:网站首页>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;
}
边栏推荐
- 小鸟识别APP
- 【pytorch】softmax函数
- Differences among tasks, threads and processes
- Class loading
- 2.3 [pytorch] data preprocessing torchvision datasets. ImageFolder
- Meituan machine test in 2022
- Shell script case in and regular expressions
- [pytorch] 2.4 convolution function nn conv2d
- [pytorch learning] torch device
- Principles of Microcomputer - internal and external structure of microprocessor
猜你喜欢

Why is the Ltd independent station a Web3.0 website!

猿人学第20题(题目会不定时更新)

FreeRTOS learning easy notes

dsPIC30F6014a LCD 方块显示

队列的实现和应用

How to manage fixed assets well? Easy to point and move to provide intelligent solutions

钓鱼识别app

Personal decoration notes

Daily practice of C language - day 80: currency change

nacos简易实现负载均衡
随机推荐
Nacos service configuration and persistence configuration
Preparing for the Blue Bridge Cup -- bit operation
How to manage fixed assets efficiently in one stop?
pcl_viewer命令
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DS18B20温度传感器 +NodeJs本地服务+ MySQL数据库
Can diffusion models be regarded as an autoencoder?
[ESP nanny level tutorial] crazy completion chapter - Case: chemical environment system detection based on Alibaba cloud and Arduino, supporting nail robot alarm
In the middle of the year, where should fixed asset management go?
易点易动助力企业设备高效管理,提升设备利用率
安装Oracle EE
Principles of Microcomputer - Introduction
Shell script - array definition and getting array elements
樹結構---二叉樹2非遞歸遍曆
Is it safe to dig up money and make new shares
Embedded Engineer Interview Question 3 Hardware
2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder
Shell script - string
Implementation and application of queue
Mysql 优化
Tree structure -- binary tree 2 non recursive traversal