Why does c++ have its separate syntax for new & delete?

梦想的初衷 提交于 2019-12-23 12:13:52

问题


Why can't it just be regular function calls? New is essentially:

malloc(sizeof(Foo));
Foo::Foo();

While delete is

Foo:~Foo();
free(...);

So why does new/delete end up having it's own syntax rather than being regular functions?


回答1:


Here's a stab at it:

The new operator calls the operator new() function. Similarly, the delete operator calls the operator delete() function (and similarly for the array versions).

So why is this? Because the user is allowed to override operator new() but not the new operator (which is a keyword). You override operator new() (and delete) to define your own allocator, however, you are not responsible (or allowed to for that matter) for calling appropriate constructors and destructors. These function are called automatically by the compiler when it sees the new keyword.

Without this dichotomy, a user could override the operator new() function, but the compiler would still have to treat this as a special function and call the appropriate constructor(s) for the object(s) being created.




回答2:


You can overload operator new and operator delete to provide your own allocation semantics. This is useful when you want to bypass the default heap allocator's behavior. For example if you allocate and deallocate a lot of instances of a small, fixed-size object, you may want to use a pool allocator for its memory management.

Having new and delete as explicit operators like other operators makes this flexibility easier to express using C++'s operator overloading mechanism.

For auto objects on the stack, allocation/constructor call and deallocation/destructor calls basically are transparent as you request. :)




回答3:


'Cause there is no way to provide complie-time type safety with a function (malloc() returns void*, remember). Additionally, C++ tries to eliminate even a slightest chance of allocated but uninitialized objects floating around. And there are objects out there without a default constructor - for these, how would you feed constructor arguments to a function? A function like this would require too much of a special-case handling; easier to promote it to a language feature. Thus operator new.




回答4:


'new/delete' are keywords in the C++ language (like 'for' and 'while'), whereas malloc/calloc are function calls in the standard C library (like 'printf' and 'sleep'). Very different beasts, more than their similar syntax may let on.

The primary difference is that 'new' and 'delete' trigger additional user code - specifically, constructors and destructors. All malloc does is set aside some memory for you to use. When setting aside memory for a simple plain old data (floats or ints, for example), 'new' and 'malloc' behave very similarly. But when you ask for space for a class, the 'new' keyword sets aside memory and then calls a constructor to initialize that class. Big difference.




回答5:


Why does C++ have separate syntax for greater-than? Why can't it just be a regular function call?

greaterThan(foo, bar);


来源:https://stackoverflow.com/questions/2286016/why-does-c-have-its-separate-syntax-for-new-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!