delete-operator

Test for void pointer in C++ before deleting

空扰寡人 提交于 2019-11-28 13:57:17
I have an array in C++: Player ** playerArray; which is initialized in the constructor of the class it is in. In the destructor I have: delete playerArray; except when testing the program through Valgrind it says that there are some calls to delete to a void pointer: operator delete(void*) I want to test whether the playerArray is a void pointer before calling delete to avoid this error. Does anyone know how to do this? Perhaps you meant delete [] playerArray . You need the [] if the pointer is an array, not a single instance. Here's how operator delete is defined. void operator delete(void*)

Delete in c++ program is not freeing up memory [closed]

我只是一个虾纸丫 提交于 2019-11-28 13:56:10
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . I created an object which has few integer variables, and one char memory block say which is allocated with memory of 300-500 bytes as its members . After that this object was pushed in to vector by one thread,

C++ assertion error while deleting object

不想你离开。 提交于 2019-11-28 13:21:29
问题 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,

Object-Oriented Suicide or delete this;

女生的网名这么多〃 提交于 2019-11-28 10:04:49
The following code compiled with MSVC9.0 runs and outputs Destructor four times, which is logical. #include <iostream> class SomeClass { public: void CommitSuicide() { delete this; } void Reincarnate() { this->~SomeClass(); new (this) SomeClass; } ~SomeClass() { std::cout << "Destructor\n"; } }; int main() { SomeClass* p = new SomeClass; p->CommitSuicide(); p = new SomeClass; p->Reincarnate(); p->~SomeClass(); //line 5 p->CommitSuicide(); } I think the first 4 lines of code in main do not result in undefined behavior (although not entirely sure about the delete this; thing). I would like to

How to control memory allocation strategy in third party library code?

拟墨画扇 提交于 2019-11-28 07:11:11
Previous header: "Must I replace global operators new and delete to change memory allocation strategy in third party code?" Short story: We need to replace memory allocation technique in third-party library without changing its source code. Long story: Consider memory-bound application that makes huge dynamic allocations (perhaps, almost all available system memory). We use specialized allocators, and use them everywhere ( shared_ptr 's, containers etc.). We have total control and power over every single byte of memory allocated in our application. Also, we need to link against a third-party

C++ Array of pointers: delete or delete []?

此生再无相见时 提交于 2019-11-28 05:15:55
Cosider the following code: class Foo { Monster* monsters[6]; Foo() { for (int i = 0; i < 6; i++) { monsters[i] = new Monster(); } } virtual ~Foo(); } What is the correct destructor? this: Foo::~Foo() { delete [] monsters; } or this: Foo::~Foo() { for (int i = 0; i < 6; i++) { delete monsters[i]; } } I currently have the uppermost constructor and everything is working okey, but of course I cannot see if it happens to be leaking... Personally, I think the second version is much more logical considering what I am doing. Anyway, what is the "proper" way to do this? delete[] monsters; Is incorrect

Delete objects of incomplete type

独自空忆成欢 提交于 2019-11-28 00:39:08
This one made me think: class X; void foo(X* p) { delete p; } How can we possibly delete p if we do not even know whether X has visible destructor? g++ 4.5.1 gives three warnings: warning: possible problem detected in invocation of delete operator: warning: 'p' has incomplete type warning: forward declaration of 'struct X' And then it says: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined. Wow... are compilers required to diagnose this situation like g++ does? Or is it undefined behavior? etarion From the

Calling delete on NULL pointers - C++03 vs C++11

孤人 提交于 2019-11-27 23:30:20
In the C++03 Standard, I see: 5.3.5 Delete 2 If the operand has a class type, the operand is converted to a pointer type by calling the above-mentioned conversion function, and the converted operand is used in place of the original operand for the remainder of this section. In either alternative, if the value of the operand of delete is the null pointer the operation has no effect. In the first alternative ( delete object ), the value of the operand of delete shall be a pointer to a non-array object or a pointer to a sub-object (1.8) representing a base class of such an object (clause 10). If

How serious is the new/delete operator mismatch error?

时光毁灭记忆、已成空白 提交于 2019-11-27 22:54:27
I have discovered the classic new/delete mismatch error in our codebase as follows: char *foo = new char[10]; // do something delete foo; // instead of delete[] foo; Just how serious is this? Does it cause a memory leak or error? What are the consequences. We have some memory issues, but this doesn't seem serious enough to explain all our symptoms (heap corruption etc) EDIT: extra questions for clarity Does it just free the first member of the array? or Does it make the system lose track of the array? or Corrupt memory is some way? It's undefined behavior serious (it could work, it could crash

How does =delete on destructor prevents allocation?

こ雲淡風輕ζ 提交于 2019-11-27 19:52:07
In this SO question is stated that this construct prevents stack allocation of instance. class FS_Only { ~FS_Only() = delete; // disallow stack allocation }; My question is, how does it prevents allocation? I understand, that is not possible to delete this instance, either explicitly or implicitly. But I think, that would lead to memory leak or run time error, respectively. Are compilers smart enough to sort this out and raise compiler error? Also why would one need this? The destructor of a variable with automatic storage duration (i.e. a local variable) would need to run when the variable's