delete-operator

Why would one replace default new and delete operators?

…衆ロ難τιáo~ 提交于 2019-11-26 14:56:35
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 critique the idea of providing an FAQ in this form, then the posting on meta that started all this would be

Legality of using operator delete on a pointer obtained from placement new

邮差的信 提交于 2019-11-26 14:23:01
问题 I'm dang certain that this code ought to be illegal, as it clearly won't work, but it seems to be allowed by the C++0x FCD. class X { /* ... */}; void* raw = malloc(sizeof (X)); X* p = new (raw) X(); // according to the standard, the RHS is a placement-new expression ::operator delete(p); // definitely wrong, per litb's answer delete p; // legal? I hope not Maybe one of you language lawyers can explain how the standard forbids this. There's also an array form: class X { /* ... */}; void* raw

Why doesn't delete destroy anything?

佐手、 提交于 2019-11-26 14:13:32
问题 I'm playing a little with memory dynamic allocation, but I don't get a point. When allocating some memory with the new statement, I'm supposed to be able to destroy the memory the pointer points to using delete . But when I try, this delete command doesn't seem to work since the space the pointer is pointing at doesn't seem to have been emptied. Let's take this truly basic piece of code as an example: #include <iostream> using namespace std; int main() { //I create a pointer-to-integer pTest,

Deleting a pointer in C++

断了今生、忘了曾经 提交于 2019-11-26 11:49:24
问题 Context: I\'m trying to wrap my head around pointers, we just saw them a couple of weeks ago in school and while practicing today I ran into a silly? issue, it can be super straightforward to you but I have little to none programming experience. I\'ve seen quite a few questions over in SO about deleting pointers but they all seem to be related to deleting a class and not a \'simple\' pointer (or whatever the proper term might be), here\'s the code I\'m trying to run: #include <iostream>;

Why I can access member functions even after the object was deleted?

假装没事ソ 提交于 2019-11-26 11:38:50
问题 I\'m new to C++ and from what I learned so far when you call delete on a pointer that points to something created on the heap then whatever is pointed by that pointer gets erased and the memory is freed, right? However when I tried this on a simple class: class MyClass { int _Id; public: MyClass(int id) : _Id(id) { std::cout << \"$Constructing the damn thing! \" << _Id << std::endl; } ~MyClass() { std::cout << \"?Destructing the damn thing! \" << _Id << std::endl; } void Go_XXX_Your_Self() {

Is the pointer guaranteed to preserve its value after `delete` in C++?

折月煮酒 提交于 2019-11-26 11:23:07
问题 Inspired by this question. Suppose in C++ code I have a valid pointer and properly delete it. According to C++ standard, the pointer will become invalid (3.7.3.2/4 - the deallocation function will render invalid all pointers referring to all parts of deallocated storage ). At least in most implementations it preserves the value and will store exactly the same address as before delete , however using the value is undefined behavior. Does the standard guarantee that the pointer will preserve

Behaviour of malloc with delete in C++

自闭症网瘾萝莉.ら 提交于 2019-11-26 11:20:30
问题 int *p=(int * )malloc(sizeof(int)); delete p; When we allocate memory using malloc then we should release it using free and when we allocate using new in C++ then we should release it using delete. But if we allocate memory using malloc and then use delete, then there should be some error. But in the above code there\'s no error or warning coming in C++. Also if we reverse and allocate using new and release using free, then also there\'s no error or warning. Why is it so? 回答1: This is

Why, really, deleting an incomplete type is undefined behaviour?

对着背影说爱祢 提交于 2019-11-26 11:12:23
问题 Consider this classic example used to explain what not to do with forward declarations: //in Handle.h file class Body; class Handle { public: Handle(); ~Handle() {delete impl_;} //.... private: Body *impl_; }; //--------------------------------------- //in Handle.cpp file #include \"Handle.h\" class Body { //Non-trivial destructor here public: ~Body () {//Do a lot of things...} }; Handle::Handle () : impl_(new Body) {} //--------------------------------------- //in Handle_user.cpp client code

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

好久不见. 提交于 2019-11-26 11:03:39
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 knows this? I mean, from the language/compiler's point of view, it has no idea whether or not arr is an

delete[] an array of objects

无人久伴 提交于 2019-11-26 09:35:12
问题 I have allocated and array of Objects Objects *array = new Objects[N]; How should I delete this array? Just delete[] array; or with iterating over the array\'s elements? for(int i=0;i<N;i++) delete array[i]; delete[]; Thanks UPDATE: I changed loop body as delete &array[i]; to force the code to compile. 回答1: Every use of new should be balanced by a delete , and every use of new[] should be balanced by delete[] . for(int i=0;i<N;i++) delete array[i]; delete[] array; That would be appropriate