开发者

What's the outcome if I use free with new or delete with malloc?

It is a compiler error or runtime error? The code below can be compiled!

class Base{
void g();
void h();
};

int main()
{
    Base* p = new Base();
    free(p);
return 0;
}

However it can't be compiled with a virtual function if I declare the class Base like this

class Base{
virtual void g();
void h();
};

The co开发者_如何学Cde below can be compiled all the time, no matter the function is virtual or not.

class Base{
void g();
void h();
};

int main()
{
    Base* p = (Base*)malloc(sizeof(Base));
    delete p;
return 0;
}


Undefined outcome, plus malloc() doesn't call constructors and free() doesn't call destructors.


As the comment says - the result is that the behaviour of the program is undefined.

If you have "new" with "free", your destructors aren't called. That typically leads to memory and resource leaks.

If you have "malloc" with "delete", you don't get constructor calls so your objects are uninitialised. That can lead to all kinds of errors, e.g. when the destructor is called.

As indicated by the comment below, there's things in Non-POD types (such as classes that have virtual methods and classes that use virtual inheritance) that need initialisation even if it's not immediately obvious that constructor initialisation is needed. If you malloc an object then call a virtual method, the most likely outcome is that your program will crash.

So far, if all your types are POD (Plain Old Data) you may get lucky, but that depends very much on your compiler - there's no guarantee that "malloc" and "new" use the same heap, so you could get heap corruption and crashes on some compilers.

In short - don't do it.


It's undefined behavior. That means anything could happen -- the program could succeed, it could crash hard, it could silently fail and crash much later in an unrelated innocent-looking piece of code, or it could even erase your hard drive.

Anything could happen, since you're doing something the C++ standard says not to do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜