delete-operator

How to properly delete pointer from a std::list?

怎甘沉沦 提交于 2019-11-30 20:20:38
问题 I'm creating an object via new , then later adding the pointer to an std::list once the object is set up. What is the correct way of deleting a pointer and erasing the data from the list without causing memory leaks? 回答1: Instead of manual loop to search the element, I would rather use std::find_if auto it = std::find_if(lst.begin(), lst.end(), [&val](datalist const &d) { return d.index == val; }); if ( it != lst.end() ) { delete *it; lst.erase(it); } That is not to say that you're doing it

Does delete[] call destructors?

佐手、 提交于 2019-11-30 18:34:59
I am writing a template class which internally manages an array of the given type. Like this: template<typename T> class Example { // ... private: T* objects; // allocated in c'tor (array), deleted in d'tor // ... }; I was wondering if C++ calls the destructor of each object in objects when I delete it via delete[] objects; . I need to know this, because the objects in my class do not contain sensible values all the time, so the destructors should not be called when they don't. Additionally, I'd like to know if the destructors would be called if I declared a fixed-sized array like T objects

How to safely delete multiple pointers

喜欢而已 提交于 2019-11-30 15:36:02
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. The answer, without resorting to managed pointers, is that you should know whether or not to delete a pointer based on where it was allocated. Your example is kind of contrived, but in a

C++ Virtual operator delete?

房东的猫 提交于 2019-11-30 13:48:26
问题 Is it possible to have a virtual delete operator? I'm not talking destructor, I mean the actual operator overload. Minus the fact that it is (in most cases) a big bad idea to overload new and delete (Yes, I already know it's heresy), I want to know what kind of implications come from using a virtual delete operator. I'm thinking about trying to use a virtual delete, as sometimes I might have a child class that overloads delete, stored in a base class pointer. Technically, I don't really ever

Parameter “size” of member operator new[] increases if class has destructor/delete[]

让人想犯罪 __ 提交于 2019-11-30 13:33:41
4 classes in the following codes: A, B, C and D. They all have a member operator new[] . Besides, B has a constructor; C has a destructor; D has a member operator delete[] . The Parameter size of member operator new[] and the sizeof of the 4 classes are output: new[] A 40 new[] B 40 new[] C 48 new[] D 48 sizeof(A) 4 sizeof(B) 4 sizeof(C) 4 sizeof(D) 4 What's the reason for the differences of size ? Codes(ugly I know): #include <iostream> using namespace std; class A { int i; public: static void* operator new[](std::size_t size) throw(std::bad_alloc) { cout << "new[] A " << size << endl; return

C++ Virtual operator delete?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 08:32:26
Is it possible to have a virtual delete operator? I'm not talking destructor, I mean the actual operator overload. Minus the fact that it is (in most cases) a big bad idea to overload new and delete (Yes, I already know it's heresy), I want to know what kind of implications come from using a virtual delete operator. I'm thinking about trying to use a virtual delete, as sometimes I might have a child class that overloads delete, stored in a base class pointer. Technically, I don't really ever see this case coming to too much fruition, unless I have a tree of different node types (potentially

Does delete[] call destructors?

守給你的承諾、 提交于 2019-11-30 03:06:45
问题 I am writing a template class which internally manages an array of the given type. Like this: template<typename T> class Example { // ... private: T* objects; // allocated in c'tor (array), deleted in d'tor // ... }; I was wondering if C++ calls the destructor of each object in objects when I delete it via delete[] objects; . I need to know this, because the objects in my class do not contain sensible values all the time, so the destructors should not be called when they don't. Additionally,

Can a compiler place the implementation of an implicitly declared virtual destructor in a single separate translation unit?

一曲冷凌霜 提交于 2019-11-29 19:34:06
问题 The following code compiles and links with Visual Studio (both 2017 and 2019 with /permissive- ), but does not compile with either gcc or clang . foo.h #include <memory> struct Base { virtual ~Base() = default; // (1) }; struct Foo : public Base { Foo(); // (2) struct Bar; std::unique_ptr<Bar> bar_; }; foo.cpp #include "foo.h" struct Foo::Bar {}; // (3) Foo::Foo() = default; main.cpp #include "foo.h" int main() { auto foo = std::make_unique<Foo>(); } My understanding is that, in main.cpp ,

Parameter “size” of member operator new[] increases if class has destructor/delete[]

元气小坏坏 提交于 2019-11-29 19:19:56
问题 4 classes in the following codes: A, B, C and D. They all have a member operator new[] . Besides, B has a constructor; C has a destructor; D has a member operator delete[] . The Parameter size of member operator new[] and the sizeof of the 4 classes are output: new[] A 40 new[] B 40 new[] C 48 new[] D 48 sizeof(A) 4 sizeof(B) 4 sizeof(C) 4 sizeof(D) 4 What's the reason for the differences of size ? Codes(ugly I know): #include <iostream> using namespace std; class A { int i; public: static

Delete and invalid pointer

最后都变了- 提交于 2019-11-29 18:27:00
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 all the elements are deleted? There are only three types of pointer values you can pass as the operand