久久久久久久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(或nilNULLfalse,具體取決于確切的數據類型).請參閱 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 中,當結果變得太高時如何計算階乘?)
主站蜘蛛池模板: 国产成人三级一区二区在线观看一 | www五月天 | 久婷婷| 国产一区二区三区精品视频 | 亚洲欧美日韩另类 | 亚洲成色www.777999 | 亚洲欧美在线播放 | 成人福利在线 | 久久久久国产精品视频 | 亚洲在线播放 | 一级特黄妇女高潮 | 国产精品一区二区不卡 | 欧美日韩免费在线 | 97国产在线视频 | 91久| www.少妇| 女教师合集乱500篇小说 | 草草视频在线观看 | 国产欧美成人 | 日韩免费网站 | 99re视频在线 | av免费看网站 | 在线观看黄色小视频 | 简单av网| 欧美日韩综合 | 国产精品96 | 手机看片国产 | 国产精品一品二区三区的使用体验 | 天天综合av| 色婷婷婷 | 激情五月综合网 | 国产裸体永久免费视频网站 | 国产精品偷拍 | 色婷婷视频在线观看 | 精品在线一区 | 亚洲小视频在线观看 | 日本国产精品 | 少妇久久久 | 久久欧美 | 免费在线观看www | 中文字幕一级片 |