小问题,关于重载delete

ed9er 2001-01-16 02:42:00
下面是Thinking in C++里的例子程序:

//: C13:GlobalOperatorNew.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Overload global new/delete
#include <cstdio>
#include <cstdlib>
using namespace std;

void* operator new(size_t sz) {
printf("operator new: %d Bytes\n", sz);
void* m = malloc(sz);
if(!m) puts("out of memory");
return m;
}

void operator delete(void* m) {
puts("operator delete");
free(m);
}

class S {
int i[100];
public:
S() { puts("S::S()"); }
~S() { puts("S::~S()"); }
};

int main() {
puts("creating & destroying an int");
int* p = new int(47);
delete p;
puts("creating & destroying an s");
S* s = new S;
delete s;
puts("creating & destroying S[3]");
S* sa = new S[3];
delete []sa;
} ///:~

然后是g++编译后运行的输出(Red Hat Linux release 6.1):

[C13]$a.out
creating & destroying an int
operator new: 4 Bytes
operator delete
creating & destroying an s
operator new: 400 Bytes
S::S()
S::~S()
operator delete
creating & destroying S[3]
operator new: 1204 Bytes
S::S()
S::S()
S::S()
S::~S()
S::~S()
S::~S()
[C13]$

前面一切都好,问题是最后delete []sa的时候怎么没有输出operator delete?free了吗?
...全文
124 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ed9er 2001-01-16
  • 打赏
  • 举报
回复
哎,急性子,没办法,才过了十五分钟我就知道了
gameboy999 2001-01-16
  • 打赏
  • 举报
回复
delete[]你又没有重载,
可能它用的是默认的那个,当然没有那句话出现了。

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧