English 中文(简体)
未调用运算符delete()的类实现
原标题:class implementation of operator delete() not being invoked

我有以下代码

#include <iostream>
#include <cstddef>
#include <string>
#include <memory>


class Object
{
public:
       Object()
       {
               std::cout << __PRETTY_FUNCTION__ << std::endl;
       }
       std::string x;

       void *operator new( size_t bytes )
       {
               std::cout << __PRETTY_FUNCTION__ << " : bytes = " << bytes << std::endl;
       }

       void operator delete( void * arg )
       {
               std::cout << __PRETTY_FUNCTION__ << std::endl;
       }
};



int main( int c, char *v[] )
{
       // std::auto_ptr< Object > pObject( new Object() );
       Object *o = new Object();
       delete o;
}

它产生这个输出。。。

static void* Object::operator new(size_t) : bytes = 8

然后是堆芯转储。

假设我没有从操作符delete()方法获得输出,并且它是核心转储。我假设我的运算符delete()方法没有被调用。

有人能解释为什么它没有被调用吗?

感谢你关注我的全帽咆哮的核心转储,因为事实证明这是个问题

EDIT-- Ok, Where do I start.... I m incredibly sorry for ranting. We ve all been there, under pressure to meet a deadline and something innocuous appears to be causing an issue and we re convinced it s one thing when in fact it s another. This has taught me a valuable lession... I need to start listening.... I fully appreciate all of help and advice given here.

Thx Mark.

最佳回答

您在调用delete()之前就崩溃了,因为您没有为<code>std::string x-如果注释掉这个实例变量,那么代码应该编译(带有警告)并运行OK。

问题回答

您的表达式做两件事。它调用适当的操作符new函数来分配一些内存,然后在运算符newObject

由于运算符new中没有返回语句,因此会得到未定义的行为。如果我们探究可能发生的情况,很可能是函数为返回值返回了一个随机值,而编译器试图在无效地址处构造Object(包括其拥有的std::string)。

这将在您的代码到达delete语句之前导致崩溃。

运算符new必须返回一个指向足够内存的指针(或引发异常),因为新表达式还将尝试为分配的内存调用Object的构造函数。问题不在于删除,而是新对象无法正常完成。

如果我将主要内容更改为

int main( int c, char *v[] )
{
       // std::auto_ptr< Object > pObject( new Object() );
       Object *o = new Object();
       std::cout<<"I m ok here"<<std::endl;
       delete o;
}

然后我得到

static void* Object::operator new(size_t) : bytes = 4
Bus error

而cout从未被称为。。这是因为你遇到了未定义的行为。(特别是,在许多编译器上,构造函数似乎是在未定义的位置调用的)

如果我变新为

   void *operator new( size_t bytes )
   {
           std::cout << __PRETTY_FUNCTION__ << " : bytes = " << bytes << std::endl;
          return new char[bytes];
   }

我明白了

static void* Object::operator new(size_t) : bytes = 4
Object::Object()
I m ok here
static void Object::operator delete(void*)

因此,如果你做了正确的事情,就会调用delete。

为什么您的操作员new没有返回值?声明返回void*,但不返回任何内容。这意味着你的编译器应该给出一个编译错误,显然没有,崩溃可能就是因为这个。

另一方面,如果这是一个例子,正如您所说,那么您可能从new返回了0,在这种情况下,不会调用运算符delete,因为在0指针上调用delete等同于空语句。

malloc new中的某个内容并返回它,然后将调用操作符delete

让我们在这里更具体地说:

  1. 你的核心转储显然是因为你没有返回一个值。

  2. <code>delete o</code>是一个删除表达式,它最终只会调用运算符delete,并且只有当指针不为NULL时才会调用。如前所述,它必须是一个有效的指针

我本以为你的构造函数也没有被调用,这只是一个线索。事实上,当你运行它时,出现了分段错误。new Object()会发生什么?分配内存(这是运算符new),然后调用构造函数。你的构造函数是如何调用的?通过取消引用指向已分配内存的指针。。。。

如果你去掉操作符new,一切都会很好:你从运行时获得内存,调用构造函数,调用运算符delete。如果您使一个实际执行任务的运算符成为新的,您将看到操作符delete(和您的构造函数)也被调用。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?