为什么删除const char *不会影响char [] [英] Why deleting const char* doesn't affect char[]

查看:68
本文介绍了为什么删除const char *不会影响char []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 - 和其他人一样 - 与const char *混淆。

我的问题就是为什么删除它不会影响char []

例如:



  char  * c =  new   char  [ 2 ]; 
c [ 0 ] = ' 1'< /跨度>;
c [ 1 ] = ' \ 0 ;
const char * d = c;
delete [] d; // 它是否也删除c?!
c [ 0 ] = ' 5';
cout<< C; // 不,c还可以!!它仍然有效



谢谢。

解决方案

首先,这是关于你的推理中的一个根本缺陷,以及对有效的理解中的这个缺陷。让我们说,我声称:不是,它是无效的(这是对的,但我稍后会解释)。您永远不应该尝试通过删除的指针访问任何内存。所以呢?这个无效应该是什么意思?如果您忽略该规则并违反该规则会发生什么?应该怎么办?我甚至无法想象你会期待什么。你希望你的电脑能吹吗?还有什么?不,你观察到的是正常的。为什么系统(堆管理器,在这种情况下)应该做任何事情来证明你的错误?即使从表现的角度来看也是错误的。因此,关键是:即使您可以访问内存(数组元素,在您的代码示例中),也不意味着您正在做一些合法的事情。你的实验根本就不能证明,显示或暗示任何东西。



到目前为止,我只解决了你的逻辑而没有解释任何事情。但我提供了一个你可以使用的提示。现在,让我解释一下。你需要理解删除的含义。你只需告诉系统:我想将使用操作符new返回的这段内存返回到自由堆;我不再需要它了。堆管理器不必修改指针或其指向的任何数据。它只是修改了它的内部结构,将内存标记为未使用。



因此,如果你试图用删除中使用的指针来寻址某些内存会发生什么?任何东西。这取决于。你不应该假设你可以使用它。在特殊和非常常见的情况下,如果您尝试访问该内存,您将看不到任何差异。但是这种内存的使用并不能保证。根据此内存的位置和OS体系结构的详细信息,您可以点击一般保护错误。但这是一个相对罕见的案例。更简单的情况是:下一个new在内存中占用相同或重叠的位置,使用该新指针的代码可能会覆盖已删除堆栈所指向的数据。你根本不能依赖任何特定的行为:在删除之后,这块堆不再是你的了。



-SA

Hi, i have - as many others do - a confusion with const char*.
Simply my question is why deleting it doesn't affect a char[]
For example:

char *c = new char[2];
c[0] = '1';
c[1] = '\0';
const char *d = c;
delete[] d; // doesn't it delete c also ?!
c[0] = '5';
cout << c; // No, c is ok!! It is still valid


Thank you.

解决方案

First of all, this is about a fundamental flaw in your reasoning, and this flaw in in the understanding of "valid". Let's say, I claim: not, it's invalid (that's right, but I'll explain it later). You should never try to access any memory by the deleted pointer. So what? What this "invalid" should mean? What happens if you ignore the rule and violate it? What should happen? I cannot even imagine what would you expect. Would you expect your computer to blow? What else? No. What you observe is normal. Why the system (heap manager, in this case) should do anything to demonstrate you your mistake? It would be wrong even from the standpoint of performance. So, the key is: even if you can access memory (array elements, in your code sample), it does not mean you are doing something legitimate. Your experiment simply does not proof, show or mean anything.

So far, I only addressed your logic and did not explain anything. But I provided a hint you could have used. Now, let me explain that. You need to understand the meaning of "delete". You simply tell the system: "I want to return this piece of memory taken using the operator new back to free heap; I won't need it anymore". The heap manager does not have to modify your pointer or any data pointed by it. It simply modifies its internal structure, marking the memory as unused.

So, what happens if you try to address some memory by a pointer which was uses in "delete"? Anything. It depends. You just should not assume you can use it. In special and a very usual case, if you try to access that memory, you won't see any difference. But the usage of this memory is just not guaranteed. Depending on location of this piece of memory and detail of OS architecture, you can hit a General Protection Fault. But this is a relatively rare case. Much simpler situation is this: next "new" takes the same or overlapping place in memory, and code using that new pointer may overwrite your data pointed by the "deleted" piece of heap. You simply cannot rely on any certain behavior: after "delete", this piece of heap is not yours anymore.

—SA


这篇关于为什么删除const char *不会影响char []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆