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

為什么全局變量和靜態變量被初始化為它們的默

Why are global and static variables initialized to their default values?(為什么全局變量和靜態變量被初始化為它們的默認值?)
本文介紹了為什么全局變量和靜態變量被初始化為它們的默認值?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在 C/C++ 中,為什么將全局變量和靜態變量初始化為默認值?

In C/C++, why are globals and static variables initialized to default values?

為什么不只留下垃圾值呢?有什么特別的嗎這是什么原因?

Why not leave it with just garbage values? Are there any special reasons for this?

推薦答案

  1. 安全:單獨保留內存會泄漏其他進程或內核的信息.

  1. Security: leaving memory alone would leak information from other processes or the kernel.

效率:這些值在初始化為某些東西之前是無用的,并且在具有展開循環的塊中將它們歸零更有效.操作系統甚至可以在系統空閑時將空閑列表頁面歸零,而不是在某些客戶端或用戶等待程序啟動時.

Efficiency: the values are useless until initialized to something, and it's more efficient to zero them in a block with unrolled loops. The OS can even zero freelist pages when the system is otherwise idle, rather than when some client or user is waiting for the program to start.

可再現性:不考慮這些值會使程序行為不可重復,從而導致很難發現錯誤.

Reproducibility: leaving the values alone would make program behavior non-repeatable, making bugs really hard to find.

優雅:如果程序可以從 0 開始,而不必使用默認初始化程序使代碼混亂,那就更干凈了.

Elegance: it's cleaner if programs can start from 0 without having to clutter the code with default initializers.

然后人們可能想知道為什么 auto 存儲類 確實 開始時是垃圾.答案有兩個:

One might then wonder why the auto storage class does start as garbage. The answer is two-fold:

  1. 它沒有,在某種意義上.每個級別的第一個堆棧幀頁面(即添加到堆棧的每個新頁面)確實收到零值.同一堆棧級別的后續函數實例看到的垃圾"或未初始化"值實際上是您自己的程序及其庫的其他方法實例留下的先前值.

  1. It doesn't, in a sense. The very first stack frame page at each level (i.e., every new page added to the stack) does receive zero values. The "garbage", or "uninitialized" values that subsequent function instances at the same stack level see are really the previous values left by other method instances of your own program and its library.

可能會有二次(或其他)運行時性能損失與將 auto(函數局部變量)初始化為任何內容相關聯.一個函數可能不會使用任何或所有大數組,比如說,在任何給定的調用中,它可能被調用數千或數百萬次.靜態和全局的初始化,OTOH,只需要發生一次.

There might be a quadratic (or whatever) runtime performance penalty associated with initializing auto (function locals) to anything. A function might not use any or all of a large array, say, on any given call, and it could be invoked thousands or millions of times. The initialization of statics and globals, OTOH, only needs to happen once.

這篇關于為什么全局變量和靜態變量被初始化為它們的默認值?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

boost_1_60_0 .zip installation in windows(Windows 中的 boost_1_60_0 .zip 安裝)
How do I get console output in C++ with a Windows program?(如何使用 Windows 程序在 C++ 中獲得控制臺輸出?)
How do I calculate the week number given a date?(如何計算給定日期的周數?)
OpenCV with Network Cameras(帶有網絡攝像機的 OpenCV)
Export all symbols when creating a DLL(創建 DLL 時導出所有符號)
Getting started with OpenCV 2.4 and MinGW on Windows 7(Windows 7 上的 OpenCV 2.4 和 MinGW 入門)
主站蜘蛛池模板: 天天插天天透 | 国产精品一区二区免费 | 天天干天天看 | 国产精品久久久久久久久久 | 国产一区高清 | 欧美成人精品欧美一级乱黄 | 日韩视频免费看 | 91av在线看 | 欧美精品乱码视频一二专区 | 久久中文网| 青娱乐av | 久久久久久亚洲 | 久久黄色大片 | 亚洲精品成人在线 | 99精品视频免费观看 | 青青草国产精品 | 不卡av在线播放 | 婷婷av在线 | 青青在线视频 | 国产乱码精品一品二品 | 国产香蕉在线 | 夜晚福利视频 | 九色视频丨porny丨丝袜 | a级片毛片| 一级片aa | 天天拍夜夜操 | 日本黄色三级视频 | 天天综合天天 | 久久99精品久久久久久琪琪 | 九九在线| 黄色a一级片 | 日韩精品一区在线观看 | av网址在线 | 日本熟妇毛耸耸xxxxxx | 免费a网站 | 欧美又粗又长 | 黄色a一级片 | 国产福利精品视频 | 亚洲小说欧美激情另类 | 美日韩一区 | 亚洲精品一区二区三区在线 |