delete-operator

Is it still safe to delete nullptr in c++0x?

孤人 提交于 2019-11-26 07:29:53
问题 In c++03 it is pretty clear that deleting a null pointer has no effect. Indeed, it is explicitly stated in §5.3.5/2 that: In either alternative, if the value of the operand of delete is the null pointer the operation has no effect. However, in the current draft for c++0x this sentence seems to be missing. In the rest of the draft I could only find sentences stating what happens if the operand of the delete-expression is not the null pointer constant. Is deleting the null pointer still defined

Well, how does the custom deleter of std::unique_ptr work?

大城市里の小女人 提交于 2019-11-26 07:21:53
问题 According to N3290 std::unique_ptr accepts a deleter argument in its constructor. However, I can\'t get that to work with Visual C++ 10.0 or MinGW g++ 4.4.1 in Windows, nor with g++ 4.6.1 in Ubuntu. I therefore fear that my understanding of it is incomplete or wrong, I can\'t see the point of a deleter argument that\'s apparently ignored, so can anyone provide a working example? Preferably I\'d like to see also how that works for unique_ptr<Base> p = unique_ptr<Derived>( new Derived ) .

Does delete on a pointer to a subclass call the base class destructor?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 06:53:13
问题 I have an class A which uses a heap memory allocation for one of its fields. Class A is instantiated and stored as a pointer field in another class ( class B . When I\'m done with an object of class B, I call delete , which I assume calls the destructor... But does this call the destructor of class A as well? Edit: From the answers, I take that (please edit if incorrect): delete of an instance of B calls B::~B(); which calls A::~A(); A::~A should explicitly delete all heap-allocated member

overloading new/delete

狂风中的少年 提交于 2019-11-26 06:39:34
问题 I\'m making a little memory leak finder in my program, but my way of overloading new and delete (and also new[] and delete[]) doesn\'t seem to do anything. void* operator new (unsigned int size, const char* filename, int line) { void* ptr = new void[size]; memleakfinder.AddTrack(ptr,size,filename,line); return ptr; } The way I overloaded new is shown in the code snippet above. I guess it\'s something with the operator returning void* but I do not know what to do about it. 回答1: void* ptr = new

Why would one replace default new and delete operators?

放肆的年华 提交于 2019-11-26 05:57:16
问题 Why should would one replace the default operator new and delete with a custom new and delete operators? This is in continuation of Overloading new and delete in the immensely illuminating C++ FAQ: Operator overloading. An followup entry to this FAQ is: How should I write ISO C++ standard conformant custom new and delete operators? Note: The answer is based on lessons from Scott Meyers\' More Effective C++. (Note: This is meant to be an entry to Stack Overflow\'s C++ FAQ. If you want to

Why is it undefined behavior to delete[] an array of derived objects via a base pointer?

一曲冷凌霜 提交于 2019-11-26 05:22:26
问题 I found the following snippet in the C++03 Standard under 5.3.5 [expr.delete] p3 : In the first alternative ( delete object ), if the static type of the object to be deleted is different from its dynamic type, the static type shall be a base class of the operand’s dynamic type and the static type shall have a virtual destructor or the behavior is undefined. In the second alternative ( delete array ) if the dynamic type of the object to be deleted differs from its static type, the behavior is

Meaning of = delete after function declaration

别等时光非礼了梦想. 提交于 2019-11-26 04:57:47
问题 class my_class { ... my_class(my_class const &) = delete; ... }; What does = delete mean in that context? Are there any other \"modifiers\" (other than = 0 and = delete )? 回答1: Deleting a function is a C++11 feature: The common idiom of "prohibiting copying" can now be expressed directly: class X { // ... X& operator=(const X&) = delete; // Disallow copying X(const X&) = delete; }; [...] The "delete" mechanism can be used for any function. For example, we can eliminate an undesired conversion

Deleting a pointer to const (T const*)

房东的猫 提交于 2019-11-26 04:44:11
问题 I have a basic question regarding the const pointers. I am not allowed to call any non-const member functions using a const pointer. However, I am allowed to do this on a const pointer: delete p; This will call the destructor of the class which in essence is a non-const \'method\'. Why is this allowed? Is it just to support this: delete this; Or is there some other reason? 回答1: It's to support: // dynamically create object that cannot be changed const Foo * f = new Foo; // use const member

Why doesn&#39;t delete set the pointer to NULL?

戏子无情 提交于 2019-11-26 03:33:48
问题 I always wondered why automatic setting of the pointer to NULL after delete is not part of the standard. If this gets taken care of then many of the crashes due to an invalid pointer would not occur. But having said that I can think of couple of reasons why the standard would have restricted this: Performance: An additional instruction could slow down the delete performance. Could it be because of const pointers. Then again standard could have done something for this special case I guess.

How does delete[] know it&#39;s an array?

不想你离开。 提交于 2019-11-26 03:29:37
问题 Alright, I think we all agree that what happens with the following code is undefined, depending on what is passed, void deleteForMe(int* pointer) { delete[] pointer; } The pointer could be all sorts of different things, and so performing an unconditional delete[] on it is undefined. However, let\'s assume that we are indeed passing an array pointer, int main() { int* arr = new int[5]; deleteForMe(arr); return 0; } My question is, in this case where the pointer is an array, who is it that