delete-operator

Can I call `delete` on a vector of pointers in C++ via for_each <algorithm>?

白昼怎懂夜的黑 提交于 2019-12-19 09:55:16
问题 Suppose I have a std::vector<Obj *> objs (for performance reasons I have pointers not actual Obj s). I populate it with obj.push_back(new Obj(...)); repeatedly. After I am done, I have to delete the pushed-back elements. One way is to do this: for (std::vector<Obj *>::iterator it = objs.begin(); it != objs.end(); ++it) { delete *it; } However, I am interested if I can use for_each algorithm to do the same: #include <algorithm> ... for_each(objs.begin(), objs.end(), delete); What do you think?

How to safely delete multiple pointers

给你一囗甜甜゛ 提交于 2019-12-18 17:13:12
问题 I have got some code which uses a lot of pointers pointing to the same address. Given a equivalent simple example: int *p = new int(1); int *q = p; int *r = q; delete r; r = NULL; // ok // delete q; q = NULL; // NOT ok // delete p; p = NULL; // NOT ok How to safely delete it without multiple delete? This is especially difficult if I have a lot of objects which having pointers all pointing to the same address. 回答1: The answer, without resorting to managed pointers, is that you should know

NULL check before deleting an object with an overloaded delete

瘦欲@ 提交于 2019-12-18 05:27:18
问题 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

Deleting array of pointers

会有一股神秘感。 提交于 2019-12-17 23:48:32
问题 Does delete[] a , where a is dynamic-allocated array of pointers, execute delete for each pointer in array? I suppose, it executes destructor for arrays with user-defined classes, but what's happening with pointers? 回答1: No, delete [] is used to delete an array. If you need to delete array elements, you need to call delete on each one of them. 回答2: No. Raw pointers contain no information about how (or whether) their target should be deallocated, so destroying one will never delete the target.

Deleting array of pointers

安稳与你 提交于 2019-12-17 23:48:12
问题 Does delete[] a , where a is dynamic-allocated array of pointers, execute delete for each pointer in array? I suppose, it executes destructor for arrays with user-defined classes, but what's happening with pointers? 回答1: No, delete [] is used to delete an array. If you need to delete array elements, you need to call delete on each one of them. 回答2: No. Raw pointers contain no information about how (or whether) their target should be deallocated, so destroying one will never delete the target.

Why do I need to delete[]?

对着背影说爱祢 提交于 2019-12-17 22:33:23
问题 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? 回答1: 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

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

只愿长相守 提交于 2019-12-17 22:11:42
问题 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

Why there is no placement delete expression in C++?

戏子无情 提交于 2019-12-17 04:28:43
问题 Why C++ hasn't placement delete that directly corresponds to the placement new, i.e. calls the destructor and calls appropriate placement delete operator? For example: MyType *p = new(arena) MyType; ... //current technique p->~MyType(); operator delete(p, arena); //proposed technique delete(arena) p; 回答1: operator delete is unique in being a non-member or static member function that is dynamically dispatched. A type with a virtual destructor performs the call to its own delete from the most

How do you 'realloc' in C++?

℡╲_俬逩灬. 提交于 2019-12-17 04:27:19
问题 How can I realloc in C++? It seems to be missing from the language - there is new and delete but not resize ! I need it because as my program reads more data, I need to reallocate the buffer to hold it. I don't think delete ing the old pointer and new ing a new, bigger one, is the right option. 回答1: Use ::std::vector! Type* t = (Type*)malloc(sizeof(Type)*n) memset(t, 0, sizeof(Type)*m) becomes ::std::vector<Type> t(n, 0); Then t = (Type*)realloc(t, sizeof(Type) * n2); becomes t.resize(n2); If

Double free or corruption after queue::push

那年仲夏 提交于 2019-12-17 04:22:45
问题 #include <queue> using namespace std; class Test{ int *myArray; public: Test(){ myArray = new int[10]; } ~Test(){ delete[] myArray; } }; int main(){ queue<Test> q Test t; q.push(t); } After I run this, I get a runtime error "double free or corruption". If I get rid of the destructor content (the delete ) it works fine. What's wrong? 回答1: Let's talk about copying objects in C++. Test t; , calls the default constructor, which allocates a new array of integers. This is fine, and your expected