delete-operator Questions
7
Solved
I am totally confused with regards to deleting things in C++. If I declare an array of objects and if I use the clear() member function. Can I be sure that the memory was released?
For example :
...
Arezzo asked 5/5, 2012 at 18:55
1
Solved
C++20 introduces the concept of a "destroying operator delete", as described below:
delete-expressions does not execute the destructor for *p before placing a call to operator delete
So...
Byproduct asked 8/9, 2020 at 15:48
8
Solved
Obj *op = new Obj;
Obj *op2 = op;
delete op;
delete op2; // What happens here?
What's the worst that can happen when you accidentally double delete? Does it matter? Is the compiler going to throw...
Inigo asked 7/2, 2012 at 1:20
4
Solved
How can I realloc in C++? It seems to be missing from the language - there is new and delete but not resize!
I need it because as my program reads more data, I need to reallocate the buffer to hol...
Heaviness asked 14/8, 2010 at 10:39
5
Solved
Consider:
delete new std :: string [2];
delete [] new std :: string;
Everyone knows the first is an error. If the second wasn't an error, we wouldn't need two distinct operators.
Now consider:
...
Solent asked 20/1, 2012 at 11:44
1
Solved
Creating a new object of class C with operator new() gives an error here:
class C
{
public:
C() {}
virtual ~C() {}
void operator delete(void*) = delete;
};
int main()
{
C* c = new C;
}
wi...
Townswoman asked 12/2, 2020 at 9:22
6
Solved
What's the equivalent of new/delete of C++ in C?
Or it's the same in C/C++?
Epanaphora asked 15/5, 2010 at 9:6
1
The following code compiles and links with Visual Studio (both 2017 and 2019 with /permissive-), but does not compile with either gcc or clang.
foo.h
#include <memory>
struct Base {
vir...
Spermatozoid asked 22/10, 2019 at 14:36
3
Solved
I tried to call ::delete for a class in the operator delete of it. But the destructor is not called.
I defined a class MyClass whose operator delete has been overloaded. The global operator delete...
Piecemeal asked 21/10, 2019 at 9:47
1
Solved
When I run this code sample in GCC and Clang
struct S
{
int a;
void *operator new(size_t s)
{
std::cout << "new " << s << std::endl;
return ::operator new(s);
}
void...
Maller asked 29/5, 2019 at 21:30
2
Solved
I was trying to work with arrays that are circular, and so ended up writing a CircularArray class for which I have attached the code. It uses a generic pointer for an array.
When I try creating a l...
Yachtsman asked 7/5, 2019 at 11:26
16
Solved
Alright, I think we all agree that what happens with the following code is undefined, depending on what is passed,
void deleteForMe(int* pointer)
{
delete[] pointer;
}
The pointer could be all ...
Indium asked 1/4, 2009 at 1:22
3
Solved
C++14 introduced "sized" versions of operator delete, i.e.
void operator delete( void* ptr, std::size_t sz );
and
void operator delete[]( void* ptr, std::size_t sz );
Reading through N3536, i...
Alderete asked 22/12, 2015 at 1:15
1
Solved
I was reading a question on SO and in one of the answers, it has been mentioned as:
If no unambiguous matching deallocation function can be found,
propagating the exception does not cause the o...
Diverting asked 4/9, 2018 at 2:56
3
Solved
What actually happen when I execute this code?
class MyClass
{
MyClass()
{
//do something
delete this;
}
}
Sanctum asked 14/3, 2011 at 19:44
7
Solved
I'm currently looking at C++ code that uses ::delete to delete a pointer.
A meaningless example of this is:
void DoWork(ExampleClass* ptr)
{
::delete ptr;
}
What is the purpose of using the d...
Costello asked 20/8, 2018 at 15:17
1
Solved
This is about non-member functions. I do understand this as an implementation. But I have a bit of puzzlement with the logic behind?
// why this?
void do_not_use_this_ever ( void ) = dele...
Pryce asked 4/8, 2018 at 6:46
5
Solved
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...
Mohun asked 1/8, 2018 at 0:5
4
If I delete a pointer as follows for example:
delete myPointer;
And, after that did not assign 0 to the pointer as follows:
myPointer = 0; //skipped this
Will myPointer be pointing to another...
Civic asked 14/2, 2011 at 9:17
7
Solved
Inspired by this question.
Suppose in C++ code I have a valid pointer and properly delete it. According to C++ standard, the pointer will become invalid (3.7.3.2/4 - the deallocation function will...
Bellaude asked 15/2, 2011 at 9:49
1
Solved
std::default_delete can be specialized to allow std::unique_ptrs to painlessly manage types which have to be destroyed by calling some custom destroy-function instead of using delete p;.
There are...
Appulse asked 10/7, 2018 at 0:12
10
Solved
I searched StackOverflow but couldn't find the answer to this question.
Suppose I have a std::vector<Day *> vector_day - that is - a vector of pointers to Day object. Now I push_back to vect...
Wordplay asked 27/7, 2010 at 15:4
1
Solved
Why would deleting an object through void* be undefined behavior, rather than compilation error?
void foo(void* p) {
delete p;
}
This code compiles and produces code, albeit with the warning on...
Deadhead asked 13/6, 2018 at 17:33
2
Solved
Pedantically, this may not be OK. As per cppref:
If expression is anything else, including if it is a pointer obtained by the array form of new-expression, the behavior is undefined.
Putting t...
Mikey asked 29/3, 2018 at 1:44
4
When I allocate a dynamic array in C++ (T * p = new T[n]), I use delete [] p to free the allocated memory. Obviously, the system knows the array size (in order among other things to call n times T'...
Zabrina asked 10/1, 2018 at 14:3
© 2022 - 2024 — McMap. All rights reserved.