delete-operator

C++ managing objects

最后都变了- 提交于 2019-12-10 17:54:54
问题 I've got a game server. It creates number of instances of Game and adds them (Or a pointer to them? What's better? It has 10 ints and 5 other pointers, 10 member functions) to the vector. After that some events happen inside of the game e. g. game_ends. At this very moment the game should be deleted from the vector... What is the best way to achieve it? The two ways we've come to are: create an observer pattern. server will be an observer and game will dispatch an event to delete it. set the

How can deleting a void pointer do anything other than invoke the global delete operator?

馋奶兔 提交于 2019-12-10 15:19:32
问题 The C++ standard very clearly and explicitly states that using delete or delete[] on a void -pointer is undefined behavior, as quoted in this answer: This implies that an object cannot be deleted using a pointer of type void* because there are no objects of type void . However, as I understand it, delete and delete[] do just two things: Call the appropriate destructor(s) Invoke the appropriate operator delete function, typically the global one There is a single-argument operator delete (as

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

孤人 提交于 2019-12-10 05:55:03
问题 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??? } 回答1: "itr" must be used like this; for (vector<foo*>::iterator

Best style for deleting all constructors (or other function)?

泄露秘密 提交于 2019-12-09 16:09:25
问题 Let's say I want to make a type that cannot be constructed (don't ask why). struct Impossible { I could do it like this: Impossible() = delete; // disable automatically generated constructors, don't declare any others Impossible(const Impossible&) = delete; // I suppose this is redundant with nothing to copy or like this: Impossible(...) = delete; // explicitly disable all constructors or like this: template<typename... Ts> Impossible(Ts...) = delete; // explicitly disable all constructors,

What is the correct way to delete char**

不打扰是莪最后的温柔 提交于 2019-12-09 14:32:53
问题 I have a char**, basically an array of strings, that I need to delete. What is the correct way of doing this to ensure all pointers are cleared up? 回答1: char** StringList ; int nStrings ; .... for (int i = 0 ; i < nStrings ; i++) delete[] StringList[i] ; delete[] StringList ; Of course, it's simpler if you start with std::vector<std::string> Stringlist ; Then it's just StringList.clear() ; 回答2: The rule of thumb is that you need one delete (or delete[] ) for each new (or new[] ) that you

How to delete void pointer?

本小妞迷上赌 提交于 2019-12-09 08:08:46
问题 Is there anything wrong when deleting an object like this in C++? MyCls* c = new MyCls(); void* p = (void*)c; delete (MyCls*)p; 回答1: This as written is legal. The cast back to MyCls* is critical. Without that, you will invoke undefined behavior--the MyCls destructor will not be called, and other problems may arise as well (such as a crash). You must cast back to the correct type. Also note that this can be complicated if multiple inheritance is involved and multiple casts are used. Your casts

Deleting a null pointer [duplicate]

放肆的年华 提交于 2019-12-08 20:38:11
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Is there any reason to check for a NULL pointer before deleting? I often see the following in code: if(pointer) delete pointer; To my understanding it is safe to delete a null pointer, so what is the point of this check? 回答1: delete will check if the pointer is NULL for you, so you're right that the check isn't needed. You might also see that some people set the pointer to NULL after it's deleted so that you don

Why shouldn't C++ operator new/delete/variants be in header files?

主宰稳场 提交于 2019-12-08 15:11:18
问题 Can someone explain the nature of this C++ compile error? I am dabbling in/learning about overloading the global operators new, delete, and their variants. I read a couple of articles on the subject, but I couldn't find one that seems to address this specifically. The Code foo.h : #ifndef foo_h #define foo_h void* operator new(size_t); void* operator new[](size_t); void operator delete(void*); void operator delete[](void*); #endif // foo_h foo.cpp : #include <foo.h> #include <iostream> void*

Freeing last element of a dynamic array

…衆ロ難τιáo~ 提交于 2019-12-08 10:09:07
问题 I have int * array=new int[2]; and I would like to free the memory of the last element , thus reducing the allocated memory to only 1 element. I tried to call delete array+1; but it gives error *** glibc detected *** skuska: free(): invalid pointer: 0x000000000065a020 * Can this be done in C++03 without explicit reallocation? Note: If I wanted to use a class instead a primitive datatype (like int), how can I free the memory so that the destructor of the class is called too? Note2: I am trying

Can I call delete on primitives?

故事扮演 提交于 2019-12-07 06:22:50
问题 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