delete-operator

Private operator delete triggers compile-time error with GCC and Clang but not with MSVC

北战南征 提交于 2019-12-04 04:08:27
问题 Motivated by this not very well asked duplicate, I believe the problem deserves a new standalone clearly titled question. The following code triggers a compilation error with GCC 8.1.0 and Clang 6.0.0, but not with MSVC 19.00: class X { public: X() /* noexcept */ { } private: static void operator delete(void*) { } }; int main() { X* x = new X{}; } From expr.new: If any part of the object initialization described above terminates by throwing an exception and a suitable deallocation function

Why is there a delete[] in C++?

隐身守侯 提交于 2019-12-04 03:33:50
Why is there a delete[] ? From my understanding its to behave differently for arrays. However, why does it really exist? There's only free in C and no free_array. Also in syntax the only difference between delete var and delete []var is the [] which has no params (I'm not telling the length of the array). So why does delete[] really exist? I know someone will say you can overload delete and delete[] (at least i think that is possible) but lets say we are not overloading it. Why does it exist? Typically, for non-POD classes, a delete[] expression must call destructors on a variable number of

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

主宰稳场 提交于 2019-12-04 03:08:52
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, template version }; I guess I could ask the same thing about any function, not just constructors. Does

Why is an overloaded delete not called when an exception is thrown in a destructor?

别说谁变了你拦得住时间么 提交于 2019-12-04 00:38:12
问题 I've written the below code which overloads the new and delete operators and throws an exception in the destructor. When the exception is thrown, why is the code in the delete operator not executed (and "bye" printed)? If it shouldn't be executed, (how) is the memory freed? Is one of the other delete operators called? Would overloading one of them instead result in the corresponding code being executed? Or is the memory simply not freed because a failed destruction implies that maybe it

What is the correct way to delete char**

回眸只為那壹抹淺笑 提交于 2019-12-04 00:10:45
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? 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() ; The rule of thumb is that you need one delete (or delete[] ) for each new (or new[] ) that you issued. So if you did: char **pp = new char*[N]; for (i = 0; i < N; i++) { pp[i] = new char[L]; } then you will need

How to delete void pointer?

梦想的初衷 提交于 2019-12-03 10:22:13
Is there anything wrong when deleting an object like this in C++? MyCls* c = new MyCls(); void* p = (void*)c; delete (MyCls*)p; 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 must "match" in either direction. If your code is structured such that you won't know the type at the time

What are some uses for =delete? [duplicate]

こ雲淡風輕ζ 提交于 2019-12-03 08:17:41
问题 This question already has an answer here : c++ syntax: default and delete modifiers (1 answer) Closed 6 years ago . Earlier today I asked a question that led to another one: When should I use =delete ? I don't think there is a post dedicated solely to =delete on SO, so I looked it up in a book called "The C++ Programming Language". I will list my findings in my answer below. Please comment or answer if there's more to say or if I'm mistaken. 回答1: It turns out that =delete is extremely useful!

Time complexity of delete[] operator [duplicate]

独自空忆成欢 提交于 2019-12-03 06:30:22
问题 This question already has answers here : Is Big-O of the C++ statement 'delete [] Q;' O(1) or O(n)? (2 answers) Closed 5 years ago . What is the Time Complexity of the delete[] operator? I mean how is it implemented - does it iterate over all the elements in the array and calls destructor for every element? Does this operator do the same for primitive types ( int , etc.) and user defined types? 回答1: ::operator delete[] is documented on cplusplus.com (which is sometimes frowned upon) as:

Why doesn't GCC optimize out deletion of null pointers in C++?

℡╲_俬逩灬. 提交于 2019-12-03 06:28:37
问题 Consider a simple program: int main() { int* ptr = nullptr; delete ptr; } With GCC (7.2), there is a call instruction regarding to operator delete in the resulting program. With Clang and Intel compilers, there are no such instructions, the null pointer deletion is completely optimized out ( -O2 in all cases). You can test here: https://godbolt.org/g/JmdoJi. I wonder whether such an optimization can be somehow turned on with GCC? (My broader motivation stems from a problem of custom swap vs

Why is it not possible to access the size of a new[]'d array?

假如想象 提交于 2019-12-03 00:13:23
When you allocate an array using new [] , why can't you find out the size of that array from the pointer? It must be known at run time, otherwise delete [] wouldn't know how much memory to free. Unless I'm missing something? In a typical implementation the size of dynamic memory block is somehow stored in the block itself - this is true. But there's no standard way to access this information. (Implementations may provide implementation-specific ways to access it). This is how it is with malloc/free , this is how it is with new[]/delete[] . In fact, in a typical implementation raw memory