I'm doing some learning exercises in c++ and I ran into an interesting question. Take this sample program.
我正在用c ++做一些学习练习,我遇到了一个有趣的问题。拿这个示例程序。
#include <string>
#include <iostream>
char* pointer1;
void temp() {
char* s1 = new char;
*s1 = 'z';
pointer1 = s1;
std::cout << *pointer1 << std::endl;
for (int i = 0; i < 90000; i++) {
// Waste some processor time.
}
}
int main() {
temp();
std::cout << *pointer1 << std::endl;
delete pointer1;
//delete &pointer1;
std::cout << *pointer1 << std::endl;
return 0;
}
When you run it it prints out 'z' twice then some random garbage. Which is what I expected. If you un-comment 'delete &pointer1' and comment out the first delete and run the program you get an invalid pointer error from the output. I'm assuming this is deleting the address and what's actually stored there is remaining.
当你运行它时,它打印出'z'两次然后打印一些随机垃圾。这是我的预期。如果你取消注释'delete&pointer1'并注释掉第一个删除并运行程序,你会从输出中得到一个无效的指针错误。我假设这是删除地址,实际存储的是剩余的。
My question is when calling 'delete pointer1' does it delete 'char* s1' or just the address to where ever the s1 is stored? When calling 'delete &pointer1' is the address being deleted but s1 still in memory?
我的问题是,当调用'delete pointer1'时它会删除'char * s1'还是只删除存储s1的地址?当调用'delete&pointer1'时,地址被删除但是s1仍在内存中?
1 个解决方案
#1
4
The address that pointer1
points to has been allocated by new
. Hence, it is correct, even necessary, to call delete
on that address.
指针1指向的地址已由new分配。因此,在该地址上调用delete是正确的,甚至是必要的。
However, &pointer1
gives you the address where pointer1
itself is stored. And this memory block has not been allocated by new
. Hence, it is strictly illegal to call delete
on it, which is precisely what the error is telling you.
但是,&pointer1为您提供存储指针1本身的地址。并且这个内存块还没有被新分配。因此,在它上面调用delete是完全违法的,这正是错误告诉你的。
So, yes, delete pointer1
frees the memory that has been allocated by new
previously. But no, delete &pointer1
doesn't do anything but being illegal.
所以,是的,删除指针1释放先前由新分配的内存。但不,删除和指针1除了非法之外什么都不做。
Additionally, accessing memory you called delete
on previously is undefined behavior. So, your second *pointer1
is also nothing you want to write in a program that you do not want to crash or, even worse, that gives you unpredictable results.
此外,访问以前称为“删除”的内存是未定义的行为。所以,你的第二个*指针1也不是你想要在你不想崩溃的程序中编写的,更糟糕的是,这会给你带来不可预测的结果。
#1
4
The address that pointer1
points to has been allocated by new
. Hence, it is correct, even necessary, to call delete
on that address.
指针1指向的地址已由new分配。因此,在该地址上调用delete是正确的,甚至是必要的。
However, &pointer1
gives you the address where pointer1
itself is stored. And this memory block has not been allocated by new
. Hence, it is strictly illegal to call delete
on it, which is precisely what the error is telling you.
但是,&pointer1为您提供存储指针1本身的地址。并且这个内存块还没有被新分配。因此,在它上面调用delete是完全违法的,这正是错误告诉你的。
So, yes, delete pointer1
frees the memory that has been allocated by new
previously. But no, delete &pointer1
doesn't do anything but being illegal.
所以,是的,删除指针1释放先前由新分配的内存。但不,删除和指针1除了非法之外什么都不做。
Additionally, accessing memory you called delete
on previously is undefined behavior. So, your second *pointer1
is also nothing you want to write in a program that you do not want to crash or, even worse, that gives you unpredictable results.
此外,访问以前称为“删除”的内存是未定义的行为。所以,你的第二个*指针1也不是你想要在你不想崩溃的程序中编写的,更糟糕的是,这会给你带来不可预测的结果。