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

代碼中的未初始化指針

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

問題描述

限時送ChatGPT賬號..

我正在學習 C++ 并且我開始知道如果未初始化的指針可能指向內存中的隨機位置并產生一些其他程序可能使用內存的問題.

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.

現在如果是這種情況,我們不應該在我們的代碼的任何部分中包含這一行:

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

int* ptr;

相反,我們應該有類似

Instead we should have something like

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

請?zhí)岢鼋ㄗh,因為我在很多書中都看到了第一行 (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

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

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)點是您可以方便地檢查 ptr 是否已初始化為任何內容,即:

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
 }

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

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.

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

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

相關文檔推薦

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++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 国产午夜精品久久 | 视频一区二区在线播放 | 一区二区不卡 | 久久高清免费视频 | 欧美国产日韩精品 | 黄色一级免费 | 99国产精品99久久久久久 | 爱爱短视频 | 黄色一区二区三区 | 国产日韩精品在线 | 日本大尺度吃奶做爰久久久绯色 | 91免费版看片 | 在线观看黄网站 | 国产精品一区久久 | 十八岁毛片 | 国产激情视频在线观看 | 99黄色| 国产一区在线视频 | 五月天婷婷综合网 | 96精品 | 三级黄色片网站 | 精品国产91 | 黄色一级小说 | 国产精品一区二区三区免费 | 国产一区在线视频 | 日韩中文字幕在线观看 | 欧美成人一级片 | 91久久久久久久久久 | 香蕉视频色 | 五月天一区二区 | 天天干天天插 | 九九久久精品视频 | 成人福利在线 | 夜间福利视频 | 色妞综合网 | 天堂成人在线 | 免费网站观看www在线观看 | 美女久久久 | 欧美国产一区二区 | 黄色一级视频网站 | 久久国产精 |