delete-operator

What are some uses for =delete? [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 20:57:50
This question already has an answer here : c++ syntax: default and delete modifiers (1 answer) 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. It turns out that =delete is extremely useful! Here are a few examples: Basically we can prevent copying base classes because it might often lead to slicing: struct Base {

Time complexity of delete[] operator [duplicate]

雨燕双飞 提交于 2019-12-02 20:03:09
This question already has an answer here: Is Big-O of the C++ statement 'delete [] Q;' O(1) or O(n)? 2 answers 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? Basile Starynkevitch ::operator delete[] is documented on cplusplus.com (which is sometimes frowned upon) as: operator delete[] can be called explicitly as a regular function, but in C++, delete[] is an operator with a very

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

…衆ロ難τιáo~ 提交于 2019-12-02 19:59:26
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 std::swap for movable types, where deletion of null pointers can represent a performance penalty in the

Valgrind does not report memory leak on “delete array”

筅森魡賤 提交于 2019-12-02 18:41:22
问题 After implementing the C++ code below, I ran valgrind --leak-check=full in order to check if there was any memory leak. The result was 0 bytes were in use at exit and no leaks are possible . However, later I discovered that I've forgotten to use delete[] x instead of just delete x inside the destructor. I searched for some explanations (for instance: delete vs delete[] operators in C++), and everything I read said that using delete without [] can cause memory leak, since it calls just the

Delete a pointer to pointer (as array of arrays)

早过忘川 提交于 2019-12-02 17:40:57
I have this in my code: double** desc = new double* [size_out]; for (int i = 0; i < size_out; i++) desc[i] = new double [size_in]; How do I delete this desc ? Should I do: delete [] desc; or for (int i=0; i<size_out; i++) delete [] desc[i]; delete [] desc; or for (int i=0; i<size_out; i++) delete [] desc[i]; delete desc; ? Simple rules to follow: for each allocation, there has to be a deallocation (ex1 is therefore wrong) what was allocated using new should be freed using delete , using new[] should be deallocated using delete[] and using malloc should be deallocated using free (ex3 is

delete modifier vs declaring function as private

非 Y 不嫁゛ 提交于 2019-12-01 15:09:28
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. 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 typically not pointing you to the source of the problem). One such case is when you add a member function that

What are “::operator new” and “::operator delete”?

ぐ巨炮叔叔 提交于 2019-12-01 12:44:53
I know new and delete are keywords. int obj = new int; delete obj; int* arr = new int[1024]; delete[] arr; <new> header is a part of C++ standard headers. It has two operators (I am not sure they are operators or they are functions): ::operator new ::operator delete these operators used like below: #include <new> using namespace std; int* buff = (int*)::operator new(1024 * sizeof(int)); ::operator delete(buff); What are "::operator new" and "::operator delete"? Are they different from new and delete keywords? Alok Save :: tells the compiler to call the operators defined in global namespace. It

Deleting an Array of Pointers - Am I doing it right?

北城以北 提交于 2019-12-01 10:56:46
I feel a little stupid for making a question about the deletion of pointers but I need to make sure I'm deleting in the correct way as I'm currently going through the debugging process of my program. Basically I have a few arrays of pointers which are defined in my header file as follows: AsteroidView *_asteroidView[16]; In a for loop I then initialise them: for(int i = 0; i < 16; i++) { _asteroidView[i] = new AsteroidView(); } Ok, so far so good, everything works fine. When I eventually need to delete these in the destructor I use this code: for(int i = 0; i < 16; i++) { delete _asteroidView

What are “::operator new” and “::operator delete”?

痞子三分冷 提交于 2019-12-01 10:32:40
问题 I know new and delete are keywords. int obj = new int; delete obj; int* arr = new int[1024]; delete[] arr; <new> header is a part of C++ standard headers. It has two operators (I am not sure they are operators or they are functions): ::operator new ::operator delete these operators used like below: #include <new> using namespace std; int* buff = (int*)::operator new(1024 * sizeof(int)); ::operator delete(buff); What are "::operator new" and "::operator delete"? Are they different from new and

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

人盡茶涼 提交于 2019-12-01 03:25:52
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 shouldn't be? #include <iostream> using namespace std; class A { public: A() { } ~A() noexcept(false) {