Visual Studio做了什么与删除的指针和为什么? [英] What does Visual Studio do with a deleted pointer and why?

查看:350
本文介绍了Visual Studio做了什么与删除的指针和为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读的C ++书中指出,当使用 delete 操作符删除指针时,它指向的位置的内存是释放的,它可以被覆盖。它还声明指针将继续指向相同的位置,直到它被重新赋值或设置为 NULL



在Visual Studio 2012中;



p> #include< iostream>

using namespace std;

int main()
{
int * ptr = new int;
cout<< ptr =<< ptr<< endl;
delete ptr;
cout<< ptr =<< ptr<< endl;

系统(pause);

return 0;
}



当我编译和运行这个程序时,我得到以下输出:

  ptr = 0050BC10 
ptr = 00008123
按任意键继续....






当调用delete时,指针指向的地址会改变! / p>

为什么会发生这种情况?这与Visual Studio有具体的关系吗?



如果删除可以改变地址,它指向反正,为什么不会自动设置指针到 NULL 而不是一些随机地址???

解决方案

存储在 ptr 中的地址总是被 00008123 ...覆盖



这似乎很奇怪,所以我做了一个小挖掘,发现这 Microsoft博客帖子,其中包含一节讨论删除C ++对象时自动指针清理。


...检查NULL是一种常见的代码结构,意味着现有的NULL检查结合使用NULL作为清除值可能偶然隐藏真正的存储器安全问题,其根本原因确实需要寻址。因为这个原因,我们选择0x8123作为清理值 - 从操作系统的角度来看,它和零地址(NULL)在同一个内存页,但是访问冲突


它不仅解释了Visual Studio使用指针的作用它被删除,它也回答为什么他们选择不自动将它设置为 NULL






此功能作为SDL检查设置的一部分启用。要启用/禁用它,请转到: PROJECT - >属性 - >配置属性 - > C / C ++ - >常规 - > SDL检查



要确认此操作:



更改此设置并重新运行相同的代码会产生以下输出:

  ptr = 007CBC10 
ptr = 007CBC10





b $ b

feature在引号中,因为在有两个指针指向同一个位置的情况下,调用delete只会清除 ONE 。另一个将指向无效位置。



Visual Studio可以通过没有在其设计中记录这个缺陷来设置一个粘滞的情况。


A C++ book I have been reading states that when a pointer is deleted using the delete operator the memory at the location it is pointing to is "freed" and it can be overwritten. It also states that the pointer will continue to point to the same location until it is reassigned or set to NULL.

In Visual Studio 2012 however; this doesn't seem to be the case!

Example:

#include <iostream>

using namespace std;

int main()
{
    int* ptr = new int;
    cout << "ptr = " << ptr << endl;
    delete ptr;
    cout << "ptr = " << ptr << endl;

    system("pause");

    return 0;
}

When I compile and run this program I get the following output:

ptr = 0050BC10
ptr = 00008123
Press any key to continue....


Clearly the address that the pointer is pointing to changes when delete is called!

Why is this happening? Does this have something to do with Visual Studio specifically?

And if delete can change the address it is pointing to anyways, why wouldn't delete automatically set the pointer to NULL instead of some random address???

解决方案

I noticed that the address stored in ptr was always being overwritten with 00008123...

This seemed odd, so I did a little digging and found this Microsoft blog post containing a section discussing "Automated pointer sanitization when deleting C++ objects".

...checks for NULL are a common code construct meaning that an existing check for NULL combined with using NULL as a sanitization value could fortuitously hide a genuine memory safety issue whose root cause really does needs addressing.

For this reason we have chosen 0x8123 as a sanitization value – from an operating system perspective this is in the same memory page as the zero address (NULL), but an access violation at 0x8123 will better stand out to the developer as needing more detailed attention.

Not only does it explain what Visual Studio does with the pointer after it is deleted, it also answers why they chose NOT to set it to NULL automatically!


This "feature" is enabled as part of the "SDL checks" setting. To enable/disable it go to: PROJECT -> Properties -> Configuration Properties -> C/C++ -> General -> SDL checks

To confirm this:

Changing this setting and rerunning the same code produces the following output:

ptr = 007CBC10
ptr = 007CBC10


"feature" is in quotes because in a case where you have two pointers to the same location, calling delete will only sanitize ONE of them. The other one will be left pointing to the invalid location.

Visual Studio could set you up for a sticky situation by failing to document this flaw in its design.

这篇关于Visual Studio做了什么与删除的指针和为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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