当前位置:网站首页>Problems caused by delete and delete[]
Problems caused by delete and delete[]
2022-07-01 09:18:00 【Clean Code 】
One 、 problem 1:delete A pointer to a array new The pointer to , Cause the program to exit abnormally
Foo* p1 = new Foo[2];
delete p1;

1、arary new The memory structure of the dynamic application is roughly shown in the figure below (vs2022)
2、 By looking at the memory layout .
3、 stay operator delete Breaking point , It is found that the pointer passed in points to the address of the first object , stay free Function causes a crash .
Compare call delete [] p1, The incoming pointer phead Is a pointing record array size Size address .
Two 、 problem 2:array delete A pointer to a new The pointer to , This causes the destructor to keep calling
Foo* p2 = new Foo;
delete [] p2;

1、new The memory structure of the dynamic application is roughly shown in the figure below (vs2022)
2、 By looking at the memory layout .
3、 stay Foo Destructor break point for , Disassembly , Find the... That controls the call destructor count It's a random value , This causes the destructor to be called continuously 
3、 ... and 、 Test code
// -------- 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; // Abnormal exit
//delete [] p1;
Foo* p2 = new Foo;
delete[] p2; // Call the destructor countless times
//delete p2;
}
边栏推荐
- 【pytorch】2.4 卷积函数 nn.conv2d
- 3D printing Arduino four axis aircraft
- Class loading
- 2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据
- Tree structure --- binary tree 1
- Design and manufacture of simple digital display electronic scale
- [ESP nanny level tutorial] crazy completion chapter - Case: temperature and humidity monitoring system based on Alibaba cloud, applet and Arduino
- js valueOf 与 toString 区别
- Which method is good for the management of fixed assets of small and medium-sized enterprises?
- Shell script -while loop explanation
猜你喜欢

JS prototype chain

NoSQL数据库的安装和使用

3D打印Arduino 四轴飞行器

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

Football and basketball game score live broadcast platform source code /app development and construction project

Which method is good for the management of fixed assets of small and medium-sized enterprises?

Ape anthropology topic 20 (the topic will be updated from time to time)

Implementation and application of queue
![[pytorch] 2.4 convolution function nn conv2d](/img/eb/382a00af5f88d5954f10ea76343d6e.png)
[pytorch] 2.4 convolution function nn conv2d

【pytorch】nn. Crossentropyloss() and nn NLLLoss()
随机推荐
The fixed assets management system enables enterprises to dynamically master assets
Reproduced Xray - cve-2017-7921 (unauthorized access by Hikvision)
js 使用toString 区分Object、Array
Shell script - string
[ESP nanny level tutorial] crazy completion chapter - Case: chemical environment system detection based on Alibaba cloud and Arduino, supporting nail robot alarm
How to manage fixed assets efficiently in one stop?
Flink interview questions
pcl_ Viewer command
Jetson Nano 安装TensorFlow GPU及问题解决
【电赛训练】红外光通信装置 2013年电赛真题
Meituan machine test in 2022
【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
JS rewrite their own functions
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DHT11 +NodeJs本地服务+ MySQL数据库
计网01-物理层
[video game training] real topic of 2013 video game of infrared optical communication device
Latex插入的eps图片模糊解决方法
js函数arguments对象
Class loading
钓鱼识别app