delete-operator

How delete a pointer of classes which has pointer members?

a 夏天 提交于 2019-12-06 07:59:41
问题 I mean, if i have some class like: class A{ int* pi; }; *A pa; when i call delete pa , will pi be deleted? 回答1: You need to define a destructor to delete pi; . In addition you also need to define a copy constructor and assignment operator otherwise when an instance of A is copied two objects will be pointing to the same int , which will be deleted when one of the instances of A is destructed leaving the other instance of A with a dangling pointer. For example: class A { public: // Constructor

The differences between free in C and delete in C++?

与世无争的帅哥 提交于 2019-12-06 04:11:59
问题 I know the free operation in C is to tell the compiler this particular memory block is free for compiler to use for further allocation, but the memory is not released. What about the delete in C++? the same as free? 回答1: There are two notions of delete in C++: One is the operator , declared as ::operator delete(void*) , which basically only frees up the memory and isn't usually thought about by most programmers. The other is the delete expression , delete p; , where p is a T* . The expression

Destructor of class with pointer array C++

血红的双手。 提交于 2019-12-05 18:13:17
If I have a class with an array of pointers to another class Vehicle : class List { public: //stuff goes here private: Vehicle ** vehicles; } If I now write the destructor of the class List , do I manually iterate over the array (I know how many items are in the array) and delete every pointer to a vehicle, or will C++ automatically call the destructors of all the Vehicles in the array? (Like it does if there's a private string/... in the class or if it would be a STL container of Vehicle pointers) EDIT: I forgot about delete [] vehicles , but if I would do that, would it also delete the

Should we delete before or after erase for an pointer in the vector?

。_饼干妹妹 提交于 2019-12-05 10:42:40
Should we delete before or after erase . My understanding is both are OK. Is it correct? In addition, is there any case we won't want to delete the element while erasing it? I believe there must be , otherwise, the erase will be happy to take the responsibility. std::vector<foo*> bar; ... for (vector<foo*>::iterator itr = bar.begin(); itr != bar.end(); itr++) { delete (*itr); //before OR bar.erase(itr); delete (*itr); //after??? } "itr" must be used like this; for (vector<foo*>::iterator itr = bar.begin(); itr != bar.end(); ) { delete (*itr); itr = bar.erase(itr); } However, I'd prefer to

Can I call delete on primitives?

徘徊边缘 提交于 2019-12-05 08:44:54
I have a templated class, myFoo, which stores "stuff" of type T which can be either primitive or pointers to complex types. When myFoo is deleted, I want to release all the memory associated with everything it happens to be storing. This means I need to call delete on every pointer being stored but I might also end up calling delete on a primitive. Is this safe?? I've included a sketch of myFoo below to better highlight what's going on. I'm not sure if the behaviour of the destructor is well defined. template<class T> class myFoo { public: myFoo(int size) { size_ = size; T* foo = new T[size_];

Delete the last node of a linked list

南楼画角 提交于 2019-12-04 21:44:20
I am trying to delete the last Node of a linked list, I have the first element of the list. But the function does not work I would be happy if you could help me Code - void deleteNode(Node* firstNode) { Node* currNode = firstNode; while (currNode->next != NULL) { currNode = currNode->next; } delete currNode->next; currNode->next = NULL; } You are deleting the one after the last node, which should be NULL anyways. void deleteNode(Node* firstNode) { //first check if firstNode is NULL or last node. if(firstNode == NULL) return; if(firstNode->next == NULL) { delete firstNode; firstNode = NULL;

Delete an object securely from a multi-threaded program

我是研究僧i 提交于 2019-12-04 21:40:28
DISCLAIMER: neither Boost, nor C++11 allowed. I've a program, in which I create an instance of Foo and I operate with it in a number of threads. Then I want to delete it securely so those threads do not fall into a segmentation fault. I've added a mutex member to Foo and lock it each time a thread function runs. In order different threads do not conflict with each other. class Foo { public: pthread_mutex_t mutex; }; void* thread ( void* fooPtr ) { Foo* fooPointer = (Foo*) fooPtr; while ( 1 ) { if ( fooPointer ) { pthread_mutex_lock ( &fooPointer->mutex ); /* some code, involving fooPointer */

Why compilers do not assign NULL to pointer variable automatically after deleting dynamic allocated memory? [duplicate]

守給你的承諾、 提交于 2019-12-04 20:51:29
This question already has an answer here: Why doesn't delete set the pointer to NULL? 12 answers Why doesn't free(p) set p to NULL? 9 answers I have small piece of code: #include <iostream> using namespace std; int main() { int *p = new int(10); if(p != NULL) { cout<<"Deleted dynamic allocated memory"<<endl; delete p; } if(p == NULL) { cout<<"NULL"<<endl; } else { cout<<"Not NULL"<<endl; } return 0; } After deleting dynamic allocated memory using delete operator, Why compilers do not assigned NULL to pointer(like p = NULL) automatically? It would often be unnecessary, particularly in well

Delete expression

自闭症网瘾萝莉.ら 提交于 2019-12-04 12:25:53
Reference here That destructor will also implicitly call the destructor of the auto_ptr object. And that will delete the pointer it holds, that points to the C object - without knowing the definition of C! That appeared in the .cpp file where struct A's constructor is defined. This was curious and then 5.3.5/5 states - "If the object being deleted has incomplete class type at the point of deletion and the complete class has a non-trivial destructor or a deallocation function, the behavior is undefined." My question is that why isn't such a program which attempts to delete a pointer to an

The differences between free in C and delete in C++?

让人想犯罪 __ 提交于 2019-12-04 09:38:27
I know the free operation in C is to tell the compiler this particular memory block is free for compiler to use for further allocation, but the memory is not released. What about the delete in C++? the same as free? There are two notions of delete in C++: One is the operator , declared as ::operator delete(void*) , which basically only frees up the memory and isn't usually thought about by most programmers. The other is the delete expression , delete p; , where p is a T* . The expression invokes the destructor of the object pointed to by p (and then frees the memory), which is a crucial