delete-operator

C++ assertion error while deleting object

∥☆過路亽.° 提交于 2019-11-29 17:39:28
I have strange assertion error and I can not find what is wrong with this code. Assertion expression is _BLOCK_TYPE_IS_VALID(pHead->nBlockUse). I simplified code a bit for better readability. class Creator { public: virtual ~Creator() { for (MyObject* item : _list) { delete item; <-- assertion error here item = 0; } _list.clear(); } template <class T> T& create() { T * item = new T(); _list.push_back(item); return *item; } private: std::list<MyObject*> _list; }; class A : public MyObject, public Creator { }; class B : public MyObject, public Creator { }; int main() { A a; a.create<A>(); } <--

Destructor in virtual inheritance

天大地大妈咪最大 提交于 2019-11-29 16:37:12
class Base{}; class D1:virtual public Base{}; class D2:virtual public Base{}; class DD:public D1,public D2{}; int main(){ Base *pBase=new DD; delete pBase; } This leads to crash, but I modify as below: class Base{ public: virtual ~Base(){}; }; class D1:virtual public Base{ public: virtual ~D1(){} }; class D2:virtual public Base{ public: virtual ~D2(){} }; class DD:public D1,public D2{ }; Then, it passes, but the default destructor should be the virtual dummy function, isn't it? From the C++11 specification (ISO/IEC 14882:2011(E)), section 12.4 Destructors [class.dtor]: Sub-section 4: If a

What happens to an address after delete operator has been applied to it in C++?

柔情痞子 提交于 2019-11-29 10:54:50
If I delete a pointer as follows for example: delete myPointer; And, after that did not assign 0 to the pointer as follows: myPointer = 0; //skipped this Will myPointer be pointing to another memory address? sharptooth No, in most implementations it will store the same address as previously - delete usually doesn't change the address and unless you assign a new address value it remains unchanged. However this is not always guaranteed . Don't forget, that doing anything except assigning a null pointer or another valid pointer to an already delete d pointer is undefined behavior - your program

NULL check before deleting an object with an overloaded delete

徘徊边缘 提交于 2019-11-29 09:05:16
This came up as one of the code review comments. Is it a good idea to check for NULL before calling delete for any object? I do understand delete operator checks for NULL internally and is redundant but the argument put forth was delete as an operator can be overloaded and if the overloaded version doesn't check for the NULL it may crash. So is it safe and reasonable to assume that if and when delete will be overloaded it will check for the NULL or not? In my understanding its reasonable to assume the first case that overloaded delete shall take care of the NULL check, and the review point

Why is delete operator required for virtual destructors

旧时模样 提交于 2019-11-29 01:47:32
In a freestanding context (no standard libraries, e.g. in operating system development) using g++ the following phenomenon occurs: class Base { public: virtual ~Base() {} }; class Derived : public Base { public: ~Derived() {} }; int main() { Derived d; } When linking it states something like this: undefined reference to operator delete(void*) Which clearly means that g++ is generating calls to delete operator even though there are zero dynamic memory allocations. This doesn't happen if destructor isn't virtual. I suspect this has to do with the generated vtable for the class but I'm not

“delete this” in constructor

十年热恋 提交于 2019-11-28 22:48:21
What actually happen when I execute this code? class MyClass { MyClass() { //do something delete this; } } It turns out that in this particular case the code is legal, but you're ε-away from undefined behavior. The C++ standard defines the notion of the "lifetime" of an object to be the time between which its constructor has finished running and when the destructor starts running. It also explicitly states (in §3.8/5) that Before the lifetime of an object has started [...] If the object will be or was of a class type with a non-trivial destructor, and the pointer is used as the operand of a

Deleting a dynamically allocated 2D array [duplicate]

我只是一个虾纸丫 提交于 2019-11-28 19:46:09
This question already has an answer here: delete vs delete[] [duplicate] 4 answers So I'm used to memory management in C where free(pointer) will free up all space pointed to by pointer . Now I'm confusing myself when attempting to do something simple in C++. If I have a 2D array of doubles allocated in a manner similar to this double** atoms = new double*[1000]; for(int i = 0; i < 1000; i++) atoms[i] = new double[4]; what would be the correct method of freeing the memory on the heap allocated by new? My thoughts were originally this (because my brain was thinking in C): for(int i = 0; i <

Why do I need to delete[]?

白昼怎懂夜的黑 提交于 2019-11-28 17:46:12
Lets say I have a function like this: int main() { char* str = new char[10]; for(int i=0;i<5;i++) { //Do stuff with str } delete[] str; return 0; } Why would I need to delete str if I am going to end the program anyways? I wouldn't care if that memory goes to a land full of unicorns if I am just going to exit, right? Is it just good practice? Does it have deeper consequences? If in fact your question really is "I have this trivial program, is it OK that I don't free a few bytes before it exits?" the answer is yes, that's fine. On any modern operating system that's going to be just fine. And

If I delete a class, are its member variables automatically deleted?

。_饼干妹妹 提交于 2019-11-28 17:33:00
I have been researching, and nothing relevant has come up, so I came here. I am trying to avoid memory leaks, so I am wondering: Say I have class MyClass with member int s a and b , and an int array c , which are filled in a member function: class MyClass { public: int a, b; int c[2]; void setVariables() { a, b = 0; for (int i = 0; i < 2; i++) { c[i] = 3; } } }; int main(int argc, char* argv[]) { MyClass* mc = new MyClass(); mc->setVariables(); delete mc; } Now, after I call delete mc , will a , b , and all the contents of c be deleted as well? Or will I have to do that explicitly in the

Delete and invalid pointer

江枫思渺然 提交于 2019-11-28 14:15:20
问题 int main() { char* a=new char[20]; cin>>a; cout<<" character at 7-th position."<<a[6]; delete a+4; cout<<a[0]; return 0; } Input: 1234567894567 Output: character at 7-th position.6 *** glibc detected *** ./test free() invalid pointer:.... Now I have 3 questions Is it correct that delete a+4 will only delete the character at a+4 ? If answer to previous one is yes then what happens to a[0] .We should get the output. to delete a chunk of memory we should write delete[] .But in this case how come