delete-operator

Why use new and delete at all?

廉价感情. 提交于 2019-11-26 21:51:53
问题 I'm new to C++ and I'm wondering why I should even bother using new and delete? It can cause problems (memory leaks) and I don't get why I shouldn't just initialize a variable without the new operator. Can someone explain it to me? It's hard to google that specific question. 回答1: For historical and efficiency reasons, C++ (and C) memory management is explicit and manual. Sometimes, you might allocate on the call stack (e.g. by using VLAs or alloca(3)). However, that is not always possible,

Calling delete on NULL pointers - C++03 vs C++11

情到浓时终转凉″ 提交于 2019-11-26 21:28:57
问题 In the C++03 Standard, I see: 5.3.5 Delete 2 If the operand has a class type, the operand is converted to a pointer type by calling the above-mentioned conversion function, and the converted operand is used in place of the original operand for the remainder of this section. In either alternative, if the value of the operand of delete is the null pointer the operation has no effect. In the first alternative ( delete object ), the value of the operand of delete shall be a pointer to a non-array

How serious is the new/delete operator mismatch error?

谁说我不能喝 提交于 2019-11-26 21:15:24
问题 I have discovered the classic new/delete mismatch error in our codebase as follows: char *foo = new char[10]; // do something delete foo; // instead of delete[] foo; Just how serious is this? Does it cause a memory leak or error? What are the consequences. We have some memory issues, but this doesn't seem serious enough to explain all our symptoms (heap corruption etc) EDIT: extra questions for clarity Does it just free the first member of the array? or Does it make the system lose track of

Does delete on a pointer to a subclass call the base class destructor?

只谈情不闲聊 提交于 2019-11-26 19:14:59
I have an class A which uses a heap memory allocation for one of its fields. Class A is instantiated and stored as a pointer field in another class ( class B . When I'm done with an object of class B, I call delete , which I assume calls the destructor... But does this call the destructor of class A as well? Edit: From the answers, I take that (please edit if incorrect): delete of an instance of B calls B::~B(); which calls A::~A(); A::~A should explicitly delete all heap-allocated member variables of the A object; Finally the memory block storing said instance of class B is returned to the

How do you 'realloc' in C++?

佐手、 提交于 2019-11-26 18:42:51
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 hold it. I don't think delete ing the old pointer and new ing a new, bigger one, is the right option. f0b0s Use ::std::vector! Type* t = (Type*)malloc(sizeof(Type)*n) memset(t, 0, sizeof(Type)*m) becomes ::std::vector<Type> t(n, 0); Then t = (Type*)realloc(t, sizeof(Type) * n2); becomes t.resize(n2); If you want to pass pointer into function, instead of Foo(t) use Foo(&t[0]) It is absolutely correct C++

Double free or corruption after queue::push

僤鯓⒐⒋嵵緔 提交于 2019-11-26 18:38:56
#include <queue> using namespace std; class Test{ int *myArray; public: Test(){ myArray = new int[10]; } ~Test(){ delete[] myArray; } }; int main(){ queue<Test> q Test t; q.push(t); } After I run this, I get a runtime error "double free or corruption". If I get rid of the destructor content (the delete ) it works fine. What's wrong? derekerdmann Let's talk about copying objects in C++. Test t; , calls the default constructor, which allocates a new array of integers. This is fine, and your expected behavior. Trouble comes when you push t into your queue using q.push(t) . If you're familiar with

Why is it undefined behavior to delete[] an array of derived objects via a base pointer?

此生再无相见时 提交于 2019-11-26 18:36:28
I found the following snippet in the C++03 Standard under 5.3.5 [expr.delete] p3 : In the first alternative ( delete object ), if the static type of the object to be deleted is different from its dynamic type, the static type shall be a base class of the operand’s dynamic type and the static type shall have a virtual destructor or the behavior is undefined. In the second alternative ( delete array ) if the dynamic type of the object to be deleted differs from its static type, the behavior is undefined. Quick review on static and dynamic types: struct B{ virtual ~B(){} }; struct D : B{}; B* p =

Meaning of = delete after function declaration

夙愿已清 提交于 2019-11-26 16:55:13
class my_class { ... my_class(my_class const &) = delete; ... }; What does = delete mean in that context? Are there any other "modifiers" (other than = 0 and = delete )? Prasoon Saurav Deleting a function is a C++11 feature : The common idiom of "prohibiting copying" can now be expressed directly: class X { // ... X& operator=(const X&) = delete; // Disallow copying X(const X&) = delete; }; [...] The "delete" mechanism can be used for any function. For example, we can eliminate an undesired conversion like this: struct Z { // ... Z(long long); // can initialize with an long long Z(long) =

Deleting a pointer to const (T const*)

浪尽此生 提交于 2019-11-26 16:13:03
I have a basic question regarding the const pointers. I am not allowed to call any non-const member functions using a const pointer. However, I am allowed to do this on a const pointer: delete p; This will call the destructor of the class which in essence is a non-const 'method'. Why is this allowed? Is it just to support this: delete this; Or is there some other reason? It's to support: // dynamically create object that cannot be changed const Foo * f = new Foo; // use const member functions here // delete it delete f; But note that the problem is not limited to dynamically created objects: {

Is there any reason to check for a NULL pointer before deleting?

对着背影说爱祢 提交于 2019-11-26 15:20:42
I often see legacy code checking for NULL before deleting a pointer, similar to, if (NULL != pSomeObject) { delete pSomeObject; pSomeObject = NULL; } Is there any reason to checking for a NULL pointer before deleting it? What is the reason for setting the pointer to NULL afterwards? It's perfectly "safe" to delete a null pointer; it effectively amounts to a no-op. The reason you might want to check for null before you delete is that trying to delete a null pointer could indicate a bug in your program. The C++ standard guarantees that it is legal to use a null pointer in a delete-expression (§8