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

整數總是初始化為0嗎?

Are ints always initialized to 0?(整數總是初始化為0嗎?)
本文介紹了整數總是初始化為0嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在 Objective-C 中指望 ints 總是被初始化為 0 是否安全?

Is it safe to count on ints always being initialized to 0 in Objective-C?

更具體地說,當新實例化具有 int ivars 的對象時,假設其 ivars 的值為 0 是否安全?

More specifically, when an object with int ivars has been newly instantiated, is it safe to assume that its ivars have value 0?

推薦答案

是的,類實例變量總是初始化為0(或nil、NULLfalse,具體取決于確切的數據類型).請參閱 Objective-C 2.0 編程語言:

Yes, class instance variables are always initialized to 0 (or nil, NULL, or false, depending on the exact data type). See the Objective-C 2.0 Programming Language:

alloc 方法為新對象的實例變量動態分配內存,并將它們全部初始化為 0 — 全部,即除了連接新實例的 isa 變量到它的類.

The alloc method dynamically allocates memory for the new object’s instance variables and initializes them all to 0—all, that is, except the isa variable that connects the new instance to its class.

<小時>

編輯 2013-05-08
蘋果似乎已經刪除了上述文件(現在鏈接到 The Wayback Machine).(當前)活動文檔 Programming With Objective-C 包含類似的引用:

alloc 方法還有另一項重要任務,即通過將對象屬性設置為零來清除分配給對象屬性的內存.這避免了內存包含以前存儲的垃圾的常見問題,但不足以完全初始化對象.

The alloc method has one other important task, which is to clear out the memory allocated for the object’s properties by setting them to zero. This avoids the usual problem of memory containing garbage from whatever was stored before, but is not enough to initialize an object completely.

<小時>

但是,這對于類的實例變量是正確的;對于在全局范圍內聲明的 POD 類型也是如此:


However, this is only true for instance variables of a class; it is also true for POD types declared at global scope:

// At global scope
int a_global_var;  // guaranteed to be 0
NSString *a_global_string;  // guaranteed to be nil

有一個例外,對于局部變量,或者對于使用 malloc()realloc() 分配的數據,它為真;calloc() 也是如此,因為 calloc() 明確地將其分配的內存清零.

With one exception, it is not true for local variables, or for data allocated with malloc() or realloc(); it is true for calloc(), since calloc() explicitly zeros out the memory it allocates.

一個例外是,當啟用自動引用計數 (ARC) 時,指向 Objective-C 對象的堆棧指針被隱式初始化為 nil;但是,將它們顯式初始化為 nil 仍然是一種好習慣.來自 過渡到 ARC 發行說明:

The one exception is that when Automatic Reference Counting (ARC) is enabled, stack pointers to Objective-C objects are implicitly initialized to nil; however, it's still good practice to explicitly initialize them to nil. From the Transitioning to to ARC Release Notes:

堆棧變量用 nil

使用 ARC,強、弱和自動釋放堆棧變量現在使用 nil

Using ARC, strong, weak, and autoreleasing stack variables are now implicitly initialized with nil

在 C++(以及 Objective-C++ 中使用的 C++ 對象)中,類實例變量也零初始化.您必須在構造函數中顯式初始化它們.

In C++ (and C++ objects being used in Objective-C++), class instance variables are also not zero-initialized. You must explicitly initialize them in your constructor(s).

這篇關于整數總是初始化為0嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How to draw stars using Quartz Core?(如何使用 Quartz Core 繪制星星?)
Why does giving addArcWithCenter a startAngle of 0 degrees make it start at 90 degrees?(為什么給 addArcWithCenter 一個 0 度的 startAngle 使它從 90 度開始?)
Convert Character to Int in Swift 2.0(在 Swift 2.0 中將字符轉換為 Int)
Edit Text shows exception Invalid Int quot;quot;(編輯文本顯示異常 Invalid Int “;)
How to convert an Int to a Character in Swift(如何在 Swift 中將 Int 轉換為字符)
In Swift 3, how to calculate the factorial when the result becomes too high?(在 Swift 3 中,當結果變得太高時如何計算階乘?)
主站蜘蛛池模板: 国产激情福利 | 日韩中文字幕在线视频观看 | 午夜视频在线免费观看 | 在线免费视频一区 | 国产激情| 国产亚洲精品精品国产亚洲综合 | 国产精品欧美精品日韩精品 | 国产乱码精品一品二品 | 久久四虎 | 国产精品五月天 | 久久免费香蕉视频 | 亚洲国产视频一区二区 | 毛片在线看看 | 国产精品久久久久久婷婷天堂 | 中文字幕一级 | 999www视频免费观看 | 久久久久亚洲国产| 亚洲 欧美 在线 一区 | 国产欧美在线视频 | 亚洲狠狠爱| 午夜三区 | 红桃成人在线 | 日本在线精品视频 | 国产成人精品一区二区三区在线 | 91精品国产乱码久久久久久久久 | 久久久久久综合 | 嫩草视频在线看 | 日韩精品一区二区三区免费观看 | 国产精品亚洲视频 | 天天干天天干 | 美女三区| 日韩在线播放一区 | 欧美激情精品久久久久久 | 狠狠亚洲 | 自拍视频精品 | 91免费在线 | 国产综合在线视频 | 午夜天堂精品久久久久 | 色免费在线视频 | 色一级 | 欧美日韩在线视频一区 |