久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

c++刪除指針問題,仍然可以訪問數據

c++ delete pointer issue, can still access data(c++刪除指針問題,仍然可以訪問數據)
本文介紹了c++刪除指針問題,仍然可以訪問數據的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我真的不明白為什么這些指針可以訪問......感謝任何幫助

I don't really understand why are those pointer accessible ... any help appreciated

#include <iostream>

class Wicked{
public:
    Wicked() {};
    virtual ~Wicked() {};

    int a;
    int b;
};


class Test
{
public:
    Test() {};
    virtual ~Test() {};

    int c;

    Wicked * TestFunc()
    {
        Wicked * z;
        c = 9;
        z = new Wicked;
        z->a = 1;
        z->b = 5;
        return z;
    };
};

int main()
{
    Wicked *z;

    Test *t = new Test();
    z = t->TestFunc();

    delete z;
    delete t;

    // why can I set 'z' when pointer is already destroyed?
    z->a = 10;

    // why does z->a print 10?
    std::cout << z->a << std::endl;

    // why does t->c exist and print correct value?
    std::cout << t->c << std::endl;

    //------------------------------------------------------

    int * p = new int;
    *p = 4;

    // this prints '4' as expected
    std::cout << *p << std::endl;

    delete p;

    // this prints memory address as expected
    std::cout << *p << std::endl;

    return 0;
}

推薦答案

刪除指針不會將任何內存清零,因為這樣做會占用 CPU 周期,而這不是 C++ 的意義所在.您所擁有的是一個懸空指針,并且可能是一個微妙的錯誤.像這樣的代碼有時可以工作多年,但在未來的某個時候,當程序中的其他地方進行了一些小的更改時,它就會崩潰.

Deleting a pointer doesn't zero out any memory because to do so would take CPU cycles and that's not what C++ is about. What you have there is a dangling pointer, and potentially a subtle error. Code like this can sometimes work for years only to crash at some point in the future when some minor change is made somewhere else in the program.

這是當您刪除指針指向的內存時應該將指針設為 NULL 的一個很好的理由,這樣如果您嘗試取消引用指針,您將立即收到錯誤消息.有時使用 memset() 之類的函數清除指向的內存也是一個好主意.如果指向的內存包含機密信息(例如明文密碼),您不希望程序的其他部分(可能面向用戶)訪問這些部分,則尤其如此.

This is a good reason why you should NULL out pointers when you've deleted the memory they point to, that way you'll get an immediate error if you try to dereference the pointer. It's also sometimes a good idea to clear the memory pointed to using a function like memset(). This is particularly true if the memory pointed to contains something confidential (e.g. a plaintext password) which you don't want other, possibly user facing, parts of your program from having access to.

這篇關于c++刪除指針問題,仍然可以訪問數據的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 国产亚洲一区二区精品 | 国产中文字幕在线观看 | 作爱视频免费看 | 久草免费在线视频 | 成人高清网站 | 欧美日本高清 | 国外成人在线视频网站 | 成人精品久久日伦片大全免费 | 97综合在线 | 久久精品国产一区老色匹 | 精品国产99 | 91麻豆蜜桃一区二区三区 | www.婷婷 | 久久精品欧美一区二区三区不卡 | 午夜免费影视 | 亚洲永久免费 | 欧美精三区欧美精三区 | 在线观看视频一区二区三区 | 亚洲一区二区久久久 | 99爱国产| 欧美一区永久视频免费观看 | jav成人av免费播放 | 久久精品99久久 | av午夜电影 | 日韩中文在线观看 | 国产成人精品免高潮在线观看 | 99精品99 | 在线播放亚洲 | 精品美女久久久久久免费 | 欧美三级电影在线播放 | 综合欧美亚洲 | 免费欧美 | 91视频在线观看 | 日韩毛片在线视频 | 丝袜美腿一区二区三区 | 亚洲视频在线播放 | 日韩国产免费观看 | 91 视频网站 | 国产一区二区三区四区 | 日本h片在线观看 | 鸳鸯谱在线观看高清 |