delete-operator

Isn't `delete[]` the counterpart to `new[]`?

隐身守侯 提交于 2020-06-01 05:32:03
问题 I'm reading to brush up on C++ knowledge that is almost 2 decades old in order to understand online info on the factory pattern. The final usage context will likely be in a different 3rd generation language (3GL), but because of my past experience, I think it's easier to follow C++ than (say) Java, even though the latter may be less intricate in syntax. A bigger reason, however, is that the only code example I can find of the problem being addressed, i.e., in the absence of the factory

C++ can operator delete fails and if not why?

你离开我真会死。 提交于 2020-05-15 10:35:09
问题 Can operator delete throw an exception or signal in some other way of error during memory de-allocation? In other way is it possible for operator delete to fail and what is it's default behavior in this case? Also what did ISO standard says about this? For example in Windows OS - C++ operator new and operator delete are normally implemented via functions HeapAlloc and HeapFree. The later function returns a boolean value which clearly indicates a fail is possible. Imagine how C++ operator

As the delete operator deallocates memory, why do I need a destructor?

こ雲淡風輕ζ 提交于 2020-05-14 18:18:05
问题 From c++ FAQ: http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.9 Remember: delete p does two things: it calls the destructor and it deallocates the memory. If delete deallocates the memory, then what's the need of the destructor here? 回答1: You need to call the destructor in case there are other things that need to be done other than just de-allocating memory. Other than very simple classes, there usually are. Things like closing file handles or shutting down database connections,

C/C++ delete vs delete[] [duplicate]

…衆ロ難τιáo~ 提交于 2020-05-13 11:40:48
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How could pairing new[] with delete possibly lead to memory leak only? delete vs delete[] I just started learning C/C++ and I was told to use delete to delete a single object and to use delete [] for an array. Then I found this website which asks this question Anything wrong with this code? T *p = new T[10]; delete p; Note: Incorrect replies: “No, everything is correct”, “Only the first element of the array will

C/C++ delete vs delete[] [duplicate]

折月煮酒 提交于 2020-05-13 11:40:12
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How could pairing new[] with delete possibly lead to memory leak only? delete vs delete[] I just started learning C/C++ and I was told to use delete to delete a single object and to use delete [] for an array. Then I found this website which asks this question Anything wrong with this code? T *p = new T[10]; delete p; Note: Incorrect replies: “No, everything is correct”, “Only the first element of the array will

How does the 'delete' operator actually work behind the scenes in C++ in dynamic memory allocation (heap)?

折月煮酒 提交于 2020-02-21 05:49:06
问题 I am not getting how the "delete" operator is actually implemented behind the scenes in C++. For example: class Node{ int i; Node *left,*right; }; int main() { Node* a = new Node; // somehow the object 'a' is initialised with its data members delete a; } What exactly does delete a; do behind the scenes? Like is there any default destructor called upon or what? Also, as a contains left and right pointers, is the object a->left and a->right also deleted? What happens at the core machine level ?

How does the 'delete' operator actually work behind the scenes in C++ in dynamic memory allocation (heap)?

社会主义新天地 提交于 2020-02-21 05:48:45
问题 I am not getting how the "delete" operator is actually implemented behind the scenes in C++. For example: class Node{ int i; Node *left,*right; }; int main() { Node* a = new Node; // somehow the object 'a' is initialised with its data members delete a; } What exactly does delete a; do behind the scenes? Like is there any default destructor called upon or what? Also, as a contains left and right pointers, is the object a->left and a->right also deleted? What happens at the core machine level ?

What happens when delete a polymorphic object without a virtual destructor?

核能气质少年 提交于 2020-02-01 08:21:27
问题 In following example, b is a polymorphic pointer type whose static type is Base* and whose dynamic type is Derived* . struct Base { virtual void f(); }; struct Derived : Base { }; int main() { Base *b = new Derived(); // ... delete b; } What happens when b is deleted without a virtual destructor? 回答1: What happens when b is deleted without a virtual destructor? We don't know. The behavior is undefined. For most actual cases the destructor of Derived might no be invoked, but nothing is

difference between new[ ] / delete [ ] vs new / delete in C++ [duplicate]

﹥>﹥吖頭↗ 提交于 2020-01-30 08:08:04
问题 This question already has answers here : Is delete[] equal to delete? (6 answers) Closed 4 years ago . I have a very quick question: What is the difference between new[ ] / delete [ ] vs new / delete in C++ when it comes to Dynamic memory? Is new[ ] / delete [ ] not belong to Dynamic memory? 回答1: new allocates memory for a single item and calls its constructor, and delete calls its destructor and frees its memory. new[] allocates memory for an array of items and calls their constructors, and

How does delete deal with pointer constness?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-30 04:29:30
问题 I was reading this question Deleting a const pointer and wanted to know more about delete behavior. Now, as per my understanding: delete expression works in two steps: invoke destructor then releases the memory (often with a call to free() ) by calling operator delete. operator delete accepts a void* . As part of a test program I overloaded operator delete and found that operator delete doesn't accept const pointer. Since operator delete does not accept const pointer and delete internally