久久久久久久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?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 日韩高清国产一区在线 | 色网站女女 | 国产黄色片在线观看 | 国产视频www | 国产精品一区二区在线免费观看 | 五月av| 可以免费看黄色的网站 | 国产成人综合在线 | 久久久久久久97 | 色婷婷婷 | 亚洲在线视频 | 日韩欧美综合 | 色老板免费视频 | 欧美成人综合 | 一区视频在线 | 四虎网站| 日本黄色视屏 | 欧美一区二区精品 | 中文在线免费观看 | 日本三级大片 | 亚洲天天操 | 欧美成人精品一区二区三区在线看 | 亚洲另类视频 | 亚洲 欧美 另类 综合 偷拍 | 精品免费在线观看 | www.久久久久久 | 亚洲砖区区免费 | 亚洲午夜在线 | 日本理伦片午夜理伦片 | 欧美日在线 | 久久久午夜精品 | 国产精品99久久久久久久久 | 黄色一级视频 | 激情久久av | 狠狠干狠狠干 | 中文字幕97 | 狠狠操天天操 | 欧美久久久久久久 | 日韩视频专区 | 久草资源在线观看 | 一级免费片 |