简体   繁体   English

将零赋给指针会破坏分配的内存吗?

[英]Does assigning zero to a pointer destroy the allocated memory?

My question may seem trivial for some people, but I don't understand why this code: 对于某些人来说,我的问题似乎微不足道,但是我不明白为什么这段代码:

double * a = new double [3];
a[0] = 1;
a[1] = 2;
a[2] = 3;

a = 0;

for(int i=0; i<3;i++)
cout << " a[" << i << "] = " << a[i] << endl;

delete [] a ; 

does not give the following result: 没有给出以下结果:

a[0] = 0;
a[1] = 0;
a[2] = 0;

The original memory allocated with new[] is leaked. 分配有new[]的原始内存已泄漏。 You now have the equivalent of 您现在有相当于

double * a = 0; //or NULL

with memory leaks. 与内存泄漏。 Accessing the null pointer will give you undefined behavior. 访问空指针将给您未定义的行为。 And no, assigning 0 to a pointer doesn't destroy the memory. 不,将0分配给指针不会破坏内存。

Your code will crash and/or produce some random result (if you assign some other constant to a ). 您的代码将崩溃和/或产生一些随机结果(如果你分配一些其他常数a )。 Why? 为什么?

In your code, a is a pointer to some region of the memory, which you are claiming for your own use (using new ). 在您的代码中, a是指向内存某些区域的指针,您声称自己可以使用(使用new )。 So you have a value of a like 0x05237484 (just a random example). 所以,你有一个值a0x05237484 (只是随便举个例子)。 So, you know that by that address 0x05237484 there are 24 bytes (3*8, 8 is the size of a double) bytes reserved for you. 因此,您知道该地址0x05237484为您保留了24个字节(3 * 8,8的大小是双0x05237484 )。 Essentially, when you say new double[5] , what you are saying to the runtime is: "now, find me a space enough to store an array of 5 doubles, and reserve it for me, so only I can use it". 本质上,当您说new double[5] ,您对运行时的意思是:“现在,找到一个足以存储5个double的数组的空间,并为我保留,所以只有我可以使用它”。 The runtime reserves memory for you and stores the address in the pointer you've provided - a 运行时内存储量为您和存储您所提供的指针的地址- a

Then, what you do is overwriting the pointer a with some other value. 然后,您要做的是用其他一些值覆盖指针a This means that the memory is still reserved for you (because you didn't tell the runtime you no longer need it, by using delete or delete[] ), but you have forgotten about where it is stored. 这意味着该内存仍为您保留(因为您没有通过使用deletedelete[]告诉运行时您不再需要该内存),但是您忘记了存储位置。

Instead, your records now point to some other memory location - where there can be anything. 而是,您的记录现在指向其他内存位置-可以有任何内容。 So, either you will get some random values from where you are now pointing (if you happen to end up within same process memory), either your program will crash saying "Access violation" - meaning you've tried to access the memory belonging to another process/system/whatever. 因此,或者您将从当前指向的位置获取一些随机值(如果您碰巧最终在同一进程内存中),或者您的程序将崩溃并显示“访问冲突”-表示您已尝试访问属于的内存另一个进程/系统/任何东西。 What you're doing here after assigning a = 0 is accessing 3 locations: 在分配a = 0之后,您在这里所做的事情是访问3个位置:

a[0] // which is a+0 == 0x00000000
a[1] // which is a+1 == 0x00000001
a[2] // which is a+2 == 0x00000002

You won't know what's at that location - but trying it, you'll either hit "access denied" error, or receive some random data (belonging to the other parts of application). 您将不知道该位置的内容-但是尝试此操作,您将遇到“访问被拒绝”错误,或者收到一些随机数据(属于应用程序的其他部分)。

And, to make things worse, you've taken some memory to you but didn't release it - this means you've eaten up from your computer RAM for nothing. 而且,更糟的是,您已经占用了一些内存但没有释放它-这意味着您已经从计算机RAM中吞噬了一切。 That's what called 'memory leak' 这就是所谓的“内存泄漏”

Assigning zero to a pointer doesn't destroy the allocated memory, that pointer stopes to point to that memory segment. 为指针分配零不会破坏分配的内存,该指针会停止指向该内存段。 And after assigning zero, delete[] will not delete that part of memory. 分配零后, delete[]不会删除该部分内存。

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

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