delete-operator

How should I write ISO C++ Standard conformant custom new and delete operators?

拥有回忆 提交于 2019-11-26 01:28:23
问题 How should I write ISO C++ standard conformant custom new and delete operators? This is in continuation of Overloading new and delete in the immensely illuminating C++ FAQ, Operator overloading, and its follow-up, Why should one replace default new and delete operators? Section 1: Writing a standard-conformant new operator Part 1: Understanding the requirements for writing a custom new operator Part 2: Understanding the new_handler requirements Part 3: Understanding specific scenario

delete vs delete[] operators in C++

怎甘沉沦 提交于 2019-11-26 01:26:17
问题 What is the difference between delete and delete[] operators in C++? 回答1: The delete operator deallocates memory and calls the destructor for a single object created with new . The delete [] operator deallocates memory and calls destructors for an array of objects created with new [] . Using delete on a pointer returned by new [] or delete [] on a pointer returned by new results in undefined behavior. 回答2: The delete[] operator is used to delete arrays. The delete operator is used to delete

Calling delete on variable allocated on the stack

那年仲夏 提交于 2019-11-26 00:33:48
问题 Ignoring programming style and design, is it \"safe\" to call delete on a variable allocated on the stack? For example: int nAmount; delete &nAmount; or class sample { public: sample(); ~sample() { delete &nAmount;} int nAmount; } 回答1: No, it is not safe to call delete on a stack-allocated variable. You should only call delete on things created by new . For each malloc or calloc , there should be exactly one free . For each new there should be exactly one delete . For each new[] there should

Is it safe to delete a NULL pointer?

耗尽温柔 提交于 2019-11-25 23:15:56
问题 Is it safe to delete a NULL pointer? And is it a good coding style? 回答1: delete performs the check anyway, so checking it on your side adds overhead and looks uglier. A very good practice is setting the pointer to NULL after delete (helps avoiding double deletion and other similar memory corruption problems). I'd also love if delete by default was setting the parameter to NULL like in #define my_delete(x) {delete x; x = NULL;} (I know about R and L values, but wouldn't it be nice?) 回答2: From

Is delete this allowed?

坚强是说给别人听的谎言 提交于 2019-11-25 22:36:52
问题 Is it allowed to delete this; if the delete-statement is the last statement that will be executed on that instance of the class? Of course I\'m sure that the object represented by the this -pointer is new ly-created. I\'m thinking about something like this: void SomeModule::doStuff() { // in the controller, \"this\" object of SomeModule is the \"current module\" // now, if I want to switch over to a new Module, eg: controller->setWorkingModule(new OtherModule()); // since the new \

Deleting array elements in JavaScript - delete vs splice

旧城冷巷雨未停 提交于 2019-11-25 21:41:55
问题 What is the difference between using the delete operator on the array element as opposed to using the Array.splice method? For example: myArray = [\'a\', \'b\', \'c\', \'d\']; delete myArray[1]; // or myArray.splice (1, 1); Why even have the splice method if I can delete array elements like I can with objects? 回答1: delete will delete the object property, but will not reindex the array or update its length. This makes it appears as if it is undefined: > myArray = ['a', 'b', 'c', 'd'] ["a", "b"

Calling delete on variable allocated on the stack

徘徊边缘 提交于 2019-11-25 19:30:57
Ignoring programming style and design, is it "safe" to call delete on a variable allocated on the stack? For example: int nAmount; delete &nAmount; or class sample { public: sample(); ~sample() { delete &nAmount;} int nAmount; } No , it is not safe to call delete on a stack-allocated variable. You should only call delete on things created by new . For each malloc or calloc , there should be exactly one free . For each new there should be exactly one delete . For each new[] there should be exactly one delete[] . For each stack allocation, there should be no explicit freeing or deletion. The