delete-operator

Delete pointer to multidimensional array in class through another pointer - how?

孤街浪徒 提交于 2019-11-27 16:27:10
I have a pointer to a class, that have a pointer to a multidimensional array but I can't seem to delete it from memory when I need to or set it to NULL. #define X 10 #define Y 10 struct TestClass { public: int *pArray[X][Y]; }; // different tries, none working: delete Pointer_To_TestClass->pArray[0][0]; delete[] Pointer_To_TestClass->pArray[0][0] // or by simply: Pointer_To_TestClass->pArray[0][0] = NULL; I know the array has data because I can see the results on screen. Also check if it's NULL already, then doesn't try to delete it. Since I want to delete a pointer in another pointer - is

Why I can access member functions even after the object was deleted?

廉价感情. 提交于 2019-11-27 15:58:11
I'm new to C++ and from what I learned so far when you call delete on a pointer that points to something created on the heap then whatever is pointed by that pointer gets erased and the memory is freed, right? However when I tried this on a simple class: class MyClass { int _Id; public: MyClass(int id) : _Id(id) { std::cout << "$Constructing the damn thing! " << _Id << std::endl; } ~MyClass() { std::cout << "?Destructing the damn thing! " << _Id << std::endl; } void Go_XXX_Your_Self() { std::cout << "%OooooooooO NOOOOOO! " << _Id << std::endl; delete this; } void Identify_Your_Self() { std:

“delete this” in constructor

折月煮酒 提交于 2019-11-27 14:27:52
问题 What actually happen when I execute this code? class MyClass { MyClass() { //do something delete this; } } 回答1: 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

Deleting a dynamically allocated 2D array [duplicate]

☆樱花仙子☆ 提交于 2019-11-27 12:29:47
问题 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

What does Visual Studio do with a deleted pointer and why?

半城伤御伤魂 提交于 2019-11-27 10:03:11
问题 A C++ book I have been reading states that when a pointer is deleted using the delete operator the memory at the location it is pointing to is "freed" and it can be overwritten. It also states that the pointer will continue to point to the same location until it is reassigned or set to NULL . In Visual Studio 2012 however; this doesn't seem to be the case! Example: #include <iostream> using namespace std; int main() { int* ptr = new int; cout << "ptr = " << ptr << endl; delete ptr; cout <<

Legality of using operator delete on a pointer obtained from placement new

一世执手 提交于 2019-11-27 08:55:54
I'm dang certain that this code ought to be illegal, as it clearly won't work, but it seems to be allowed by the C++0x FCD. class X { /* ... */}; void* raw = malloc(sizeof (X)); X* p = new (raw) X(); // according to the standard, the RHS is a placement-new expression ::operator delete(p); // definitely wrong, per litb's answer delete p; // legal? I hope not Maybe one of you language lawyers can explain how the standard forbids this. There's also an array form: class X { /* ... */}; void* raw = malloc(sizeof (X)); X* p = new (raw) X[1]; // according to the standard, the RHS is a placement-new

Why doesn't delete destroy anything?

扶醉桌前 提交于 2019-11-27 08:38:00
I'm playing a little with memory dynamic allocation, but I don't get a point. When allocating some memory with the new statement, I'm supposed to be able to destroy the memory the pointer points to using delete . But when I try, this delete command doesn't seem to work since the space the pointer is pointing at doesn't seem to have been emptied. Let's take this truly basic piece of code as an example: #include <iostream> using namespace std; int main() { //I create a pointer-to-integer pTest, make it point to some new space, // and fulfill this free space with a number; int* pTest; pTest = new

Why, really, deleting an incomplete type is undefined behaviour?

帅比萌擦擦* 提交于 2019-11-27 08:16:05
Consider this classic example used to explain what not to do with forward declarations: //in Handle.h file class Body; class Handle { public: Handle(); ~Handle() {delete impl_;} //.... private: Body *impl_; }; //--------------------------------------- //in Handle.cpp file #include "Handle.h" class Body { //Non-trivial destructor here public: ~Body () {//Do a lot of things...} }; Handle::Handle () : impl_(new Body) {} //--------------------------------------- //in Handle_user.cpp client code: #include "Handle.h" //... in some function... { Handle handleObj; //Do smtg with handleObj... /

Test for void pointer in C++ before deleting

夙愿已清 提交于 2019-11-27 08:01:44
问题 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? 回答1: Perhaps you meant delete [] playerArray . You need the [] if the

delete modifier vs declaring function as private

落爺英雄遲暮 提交于 2019-11-27 06:37:46
问题 I read this question, but it still doesn't make a lot of sense to me. It still sounds more like a sugarcoating feature. What's the difference between: class A { // public/private ? A (const A&) = delete; }; and class A { private: A (const A&); // MISSING implementation }; Same for operator= or other functions. 回答1: One difference is that =delete allows for compile-time errors while in some cases the declaration without a definition is only caught at link-time (at which the error message is