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

代碼中的未初始化指針

Uninitialized pointers in code(代碼中的未初始化指針)
本文介紹了代碼中的未初始化指針的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我正在學(xué)習(xí) C++ 并且我開始知道如果未初始化的指針可能指向內(nèi)存中的隨機(jī)位置并產(chǎn)生一些其他程序可能使用內(nèi)存的問題.

I am learning C++ and I came to know that pointers if left uninitialized could point to random locations in memory and create problems that memory might be used by some other program.

現(xiàn)在如果是這種情況,我們不應(yīng)該在我們的代碼的任何部分中包含這一行:

Now if that is the case we should never have this line in any part of our code:

int* ptr;

相反,我們應(yīng)該有類似

Instead we should have something like

int* ptr = NULL; //Is this going to avoid the problem

請?zhí)岢鼋ㄗh,因?yàn)槲以诤芏鄷卸伎吹搅说谝恍?(int* ptr;) 所以我有這個疑問.如果可能,還可以舉一些例子.

Please suggest because I have seen the first line(int* ptr;) in many books so I am getting this doubt. If possible give some examples also.

推薦答案

int* ptr = NULL; //Is this going to avoid the problem

這將導(dǎo)致 ptr 指向 NULL,您可以將其明確檢查為默認(rèn)/未初始化值.它可以防止您描述的問題,但是粗心的程序員仍然可以在不檢查的情況下意外取消引用空指針,從而導(dǎo)致未定義的行為.

This will cause ptr to point to NULL which you can explicitly check for as a default/uninitialized value. It prevents the problem you describe, but a careless programmer can still accidentally dereference a null pointer without checking, causing undefined behaviour.

主要優(yōu)點(diǎn)是您可以方便地檢查 ptr 是否已初始化為任何內(nèi)容,即:

The main advantage is your convenience for checking whether the ptr has or has not been initialized to anything, ie:

 if (ptr != NULL)
 {
     // assume it points to something
 }

因?yàn)檫@是非常慣用的,所以不將指針初始化為 NULL 是非常危險的.指針將被初始化為一個非 NULL 垃圾值,它并不真正指向任何真實(shí)的東西.最糟糕的是,上面的檢查會通過,如果碰巧指針中的地址是您可以合法訪問的內(nèi)存,則會導(dǎo)致更嚴(yán)重的問題.在某些嵌入式環(huán)境中,您可能能夠訪問內(nèi)存的任何部分,因此您可能會意外損壞內(nèi)存的隨機(jī)部分或執(zhí)行代碼的隨機(jī)部分.

Since this is pretty idiomatic, its pretty dangerous to not initialize the pointer to NULL. The pointer would be initialized to a non-NULL garbage value that doesn't really point to anything real. Worst of all, the check above would pass, causing even worse problems if it just so happens that the address in the pointer is memory you can legally access. In some Embedded environments, you might be able to access any part of memory, so you might accidentally corrupt random parts of memory or random parts of your executing code.

這篇關(guān)于代碼中的未初始化指針的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區(qū)別?)
Difference between const. pointer and reference?(常量之間的區(qū)別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內(nèi)容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進(jìn)行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導(dǎo)致訪問沖突?)
主站蜘蛛池模板: 欧美久久久久久久久 | 三级高清 | 一本一道久久a久久精品综合蜜臀 | 在线看片福利 | 亚洲精品九九 | 九九九久久国产免费 | 中文字幕免费 | 国产一二三区免费视频 | 久久久久久久久中文字幕 | 欧洲精品一区 | 国产日韩在线观看一区 | 黄色国产视频 | 日韩中文字幕在线不卡 | 很黄很污的网站 | 狠狠色香婷婷久久亚洲精品 | 精品日韩在线观看 | 国产激情视频网址 | 欧美久久久久久 | 日韩视频91 | 欧美性生活免费 | 久久乐国产精品 | 久国产| 天天干天天操天天射 | 欧美视频偷拍 | 久草色播 | 亚洲va欧美va天堂v国产综合 | 中文字字幕一区二区三区四区五区 | 成人av免费 | 国产精品日韩一区二区 | 精品免费看 | 亚洲精品久久久久中文字幕欢迎你 | 99九九久久| 视频在线观看一区二区 | 欧美一级视频免费看 | 日韩一区二区三区视频在线播放 | 伊人伊成久久人综合网站 | 国产免费视频 | 在线免费观看视频你懂的 | 91久久国产精品 | 一区二区三区在线 | 成人午夜视频在线观看 |