久久久久久久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?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 大乳boobs巨大吃奶挤奶 | 亚洲国产一区二区三区在线观看 | 欧美视频日韩 | 久久精品国产亚洲 | 中文字幕的av | 欧美综合国产精品久久丁香 | 精品久久99 | 日本一区高清 | 中文字幕高清 | 欧美日韩高清一区 | 男人视频网站 | 国产乱码精品一区二三赶尸艳谈 | 成人午夜免费在线视频 | www.狠狠干 | 国产在线不卡视频 | 九九免费观看视频 | 精品国产一区二区三区免费 | 国产在线一区二 | 欧美成人免费在线视频 | 欧美黄色一区 | 日韩精品二区 | 亚洲精品欧美一区二区三区 | 日韩国产在线 | 九九久久久 | 日本精品999 | 人人做人人澡人人爽欧美 | 日韩欧美国产精品 | 欧美日韩一区二区视频在线观看 | 福利精品| 亚洲一区二区三区四区五区中文 | 免费在线观看成人 | 五月天婷婷综合 | 91免费版在线观看 | 91九色在线观看 | 精品一区二区三区中文字幕 | 久久久久一区二区 | 国产91在线 | 亚洲 | 九九热免费视频在线观看 | 亚洲精品久久久一区二区三区 | 天堂久久av | 亚洲精品粉嫩美女一区 |