删除用指针分配的内存(动态数组)
Deleting memory allocated with pointers (dynamic array)
如果我创建 2 个指针:
int *pointer;
int *temp;
我在这种情况下为其中之一分配内存 temp
:
temp = new int [ size ]; //size = 6
然后我将第二个指针 pointer
指向相同的内存位置:
pointer = temp;
如果我想解除临时分配:
delete [ ] temp;
它不会让我...我说它不会让我因为如果我做一个循环来填充 temp
的元素它仍然会让我填充它即使在我使用 delete [ ] temp;
.我想了解这里发生了什么,所以现在我的问题是:
- 当我这样做时
delete [ ] temp;
是内存被释放(我想不是因为我有另一个指针指向那个位置)如果不是……这是否意味着 delete [ ] temp;
没有效果?
- 如果有多个指针指向该内存位置,是否可以取消分配内存?
- 假设我已经完成了这两个指针,我想释放内存,最好的方法是什么?我不能做
delete [ ] pointers;
因为我从来没有为指针分配内存我只是将它指向 temp
内存地址。
注意:我知道我可以使用向量我只是想在使用指针时了解内存管理。
When I did delete [ ] temp;
was the memory deallocated
当然是! delete
运算符非常听话,它假设如果你要求它删除一块内存,那么你就永远完成了那块内存。
I think not because I had another pointer pointing to that location
当您在 temp
上调用 delete []
时,其他指针变为 悬空 。取消引用它是 undefined behavior,这意味着你的程序是无效的,即使它编译甚至运行完成而没有崩溃。
Is it possible to deallocate memory if more than one pointer is pointing to that memory location?
是的,也就是,事实上,会发生什么。
Lets say that i am done with both pointers and i want to free the memory, what would be the best way to do it?
使用智能指针,例如std::shared_ptr<T>
。这将使您避免完全调用 delete
。
When I did delete [ ] temp;
was the memory deallocated
是的。使用删除后的内存会导致undefined behavior,即任何事情都可能发生,包括但不限于:代码编译、代码崩溃、代码按预期运行……
if i do a loop to populate elements of temp
I will still let me populate it even after I used delete [ ] temp;
C++ 没有防止删除后使用的机制。如果您这样做,那么您只是在调用未定义的行为。
Is it possible to deallocate memory if more than one pointer is pointing to that memory location?
是的。同样,C++ 中没有任何内容可以阻止程序员这样做。您必须确保您的代码不会重复删除任何内容或访问任何已被释放的内容。
I cannot do delete [ ] pointers;
because I never allocated memory for pointer I just pointed it to the temp memory address.
你也可以通过pointer
删除记忆。任何指向已分配内存 且与用于分配内存的类型相同的指针 都可用于删除它。
最后,按照 dasblinkenlight 所说:避免像 new
和 delete
这样的高级手动内存处理,而更喜欢 智能指针 。它们会自动释放您的内存 1) 在正确的时间,以及 2) 仅释放一次。之后,他们将自己重置为 nullptr
,使删除后使用变得容易发现。
如果我创建 2 个指针:
int *pointer;
int *temp;
我在这种情况下为其中之一分配内存 temp
:
temp = new int [ size ]; //size = 6
然后我将第二个指针 pointer
指向相同的内存位置:
pointer = temp;
如果我想解除临时分配:
delete [ ] temp;
它不会让我...我说它不会让我因为如果我做一个循环来填充 temp
的元素它仍然会让我填充它即使在我使用 delete [ ] temp;
.我想了解这里发生了什么,所以现在我的问题是:
- 当我这样做时
delete [ ] temp;
是内存被释放(我想不是因为我有另一个指针指向那个位置)如果不是……这是否意味着delete [ ] temp;
没有效果? - 如果有多个指针指向该内存位置,是否可以取消分配内存?
- 假设我已经完成了这两个指针,我想释放内存,最好的方法是什么?我不能做
delete [ ] pointers;
因为我从来没有为指针分配内存我只是将它指向temp
内存地址。
注意:我知道我可以使用向量我只是想在使用指针时了解内存管理。
When I did
delete [ ] temp;
was the memory deallocated
当然是! delete
运算符非常听话,它假设如果你要求它删除一块内存,那么你就永远完成了那块内存。
I think not because I had another pointer pointing to that location
当您在 temp
上调用 delete []
时,其他指针变为 悬空 。取消引用它是 undefined behavior,这意味着你的程序是无效的,即使它编译甚至运行完成而没有崩溃。
Is it possible to deallocate memory if more than one pointer is pointing to that memory location?
是的,也就是,事实上,会发生什么。
Lets say that i am done with both pointers and i want to free the memory, what would be the best way to do it?
使用智能指针,例如std::shared_ptr<T>
。这将使您避免完全调用 delete
。
When I did
delete [ ] temp;
was the memory deallocated是的。使用删除后的内存会导致undefined behavior,即任何事情都可能发生,包括但不限于:代码编译、代码崩溃、代码按预期运行……
if i do a loop to populate elements of
temp
I will still let me populate it even after I useddelete [ ] temp;
C++ 没有防止删除后使用的机制。如果您这样做,那么您只是在调用未定义的行为。
Is it possible to deallocate memory if more than one pointer is pointing to that memory location?
是的。同样,C++ 中没有任何内容可以阻止程序员这样做。您必须确保您的代码不会重复删除任何内容或访问任何已被释放的内容。
I cannot do
delete [ ] pointers;
because I never allocated memory for pointer I just pointed it to the temp memory address.你也可以通过
pointer
删除记忆。任何指向已分配内存 且与用于分配内存的类型相同的指针 都可用于删除它。最后,按照 dasblinkenlight 所说:避免像
new
和delete
这样的高级手动内存处理,而更喜欢 智能指针 。它们会自动释放您的内存 1) 在正确的时间,以及 2) 仅释放一次。之后,他们将自己重置为nullptr
,使删除后使用变得容易发现。