简体   繁体   English

C++:memory 当我删除由 new 创建的指针时出现问题

[英]C++: memory issue when I delete a pointer created by new

I'm a little bit confused by the following code我对以下代码有点困惑

int main()
{
    int* a = new int{12};
    int* b = new int;

    b = a;
    delete a;

    delete b;

    return 0;
}

The code returns an error that代码返回一个错误

a.out(27538,0x10ade7e00) malloc: *** error for object 0x7f8c18504160: pointer being freed was not allocated
a.out(27538,0x10ade7e00) malloc: *** set a breakpoint in malloc_error_break to debug
zsh: abort      ./a.out

My question is that, when I delete the a, does it automatically delete b?我的问题是,当我删除a时,它会自动删除b吗? What is the mechanism here, I'm a little bit getting lost.这里的机制是什么,我有点迷路了。

b = a;    // here the int that b pointed at "leaks" (you have no way of deleting it)

delete a; // here "a" is deleted first

delete b; // here "a" is deleted a second time (undefined behavior)

When you assign the value of a to b , the value (address if you will) b previously contained, is forgotten.当您将a的值分配给b时,先前包含的值(如果您愿意的话) b将被遗忘。 Both a and b then points at the same object.然后ab都指向同一个 object。 You then lost all possibilities to delete the original object b pointed at.然后,您失去了delete指向的原始 object b的所有可能性。 When you then delete b you in fact try deleting a a second time.然后,当您delete b时,您实际上尝试再次删除a

Let's walk through the code line by line.让我们逐行浏览代码。

int* a = new int{12};

This creates a new variable called a of type int* and initializes it to point to a newly-allocated integer assigned the value 12.这将创建一个名为aint*类型的新变量,并将其初始化为指向新分配的 integer,其值为 12。

int* b = new int;

This creates a new variable called b of type int* and initializes it to point to a newly-allocated integer.这将创建一个名为bint*类型的新变量,并将其初始化为指向新分配的 integer。

b = a;

This changes b 's value to point to a .这会将b的值更改为指向a The value returned in the second call to new is now lost and that memory is leaked since it is no longer possible to pass it to delete .在对new的第二次调用中返回的值现在丢失了,并且 memory 被泄露,因为它不再可能传递给delete

delete a;

This deletes the object a points to, the one allocated first.这将删除 object 指向的a指向,即首先分配的那个。

delete b;

Oops, this tries to delete the object b points to, but b doesn't point to any object that currently exists.糟糕,这试图删除b指向的 object,但b不指向当前存在的任何 object。 The one allocated first was just deleted and no pointer to the second one exists.第一个分配的那个刚刚被删除,并且不存在指向第二个的指针。 So this is a bug.所以这是一个错误。

I suspect you are thinking that delete a;我怀疑你在想delete a; deletes a .删除a . It does not.它不是。 It deletes whatever object a points to and requires that a be a pointer and point to a valid object that was allocated with new .它会删除任何 object a指向的内容,并要求a是一个指针并指向一个有效的 object ,该有效的 object 被分配了new

When you delete a pointer, all pointers, references, iterators etc. pointing to the destroyed object are invalidated.当您删除一个指针时,所有指向被破坏的 object 的指针、引用、迭代器等都将失效。 Because of the assignment b = a , b points to the same object as a does, so when a is deleted, b becomes invalid.由于赋值b = a , b 指向与 a 相同的 object ,所以当 a 被删除时, b 变得无效。 Deleting an invalid pointer has undefined behaviour.删除无效指针具有未定义的行为。

Note that b = a makes it impossible to delete the allocation that b previously pointed at.请注意, b = a使得无法删除 b 先前指向的分配。 This is called a memory leak.这称为 memory 泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM