objective-c 整数总是初始化为 0 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/990817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:42:58  来源:igfitidea点击:

Are ints always initialized to 0?

objective-cinitializationintinstance-variables

提问by Felixyz

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

int在 Objective-C 中指望s 总是被初始化为 0是否安全?

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

更具体地说,当一个带有intivars的对象被新实例化时,假设它的 ivars 值为 0 是否安全?

回答by Adam Rosenfield

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:

是的,类实例变量始终初始化为 0(或nilNULL、 或false,具体取决于确切的数据类型)。请参阅Objective-C 2.0 编程语言

The allocmethod dynamically allocates memory for the new object's instance variables and initializes them all to 0—all, that is, except the isavariable that connects the new instance to its class.

alloc方法为新对象的实例变量动态分配内存,并将它们全部初始化为 0——所有,isa即连接新实例与其类的变量除外。



EDIT 2013-05-08
Apple seems to have removed the above document (now linked to The Wayback Machine). The (currently) active document Programming With Objective-Ccontains a similar citation:

编辑 2013-05-08
Apple 似乎已经删除了上述文档(现在链接到 The Wayback Machine)。(当前)活动文档Programming With Objective-C包含类似的引文:

The allocmethod 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.

alloc方法还有另一项重要任务,即通过将对象属性设置为零来清除为对象属性分配的内存。这避免了内存包含来自之前存储的任何内容的垃圾的常见问题,但不足以完全初始化对象。



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

但是,这适用于类的实例变量;对于在全局范围内声明的 POD 类型也是如此:

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

With one exception, it is nottrue 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.

除了一个例外,它不适用于局部变量,或者用malloc()或分配的数据realloc();对于 来说是这样calloc(),因为calloc()显式地将它分配的内存清零。

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:

一个例外是,当启用自动引用计数 (ARC) 时,指向 Objective-C 对象的堆栈指针被隐式初始化为nil; 但是,将它们显式初始化为nil. 从过渡到 ARC 发行说明

Stack Variables Are Initialized with nil

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

堆栈变量初始化为 nil

使用 ARC,强、弱和自动释放堆栈变量现在隐式初始化为 nil

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

在 C++(以及在 Objective-C++ 中使用的 C++ 对象)中,类实例变量也不是零初始化的。您必须在构造函数中显式初始化它们。

回答by Cody C

I don't think you should assume any values for initialization. If you are building logic around a "0" value, you should set it to be sure.

我认为您不应该为初始化假设任何值。如果您围绕“0”值构建逻辑,则应将其设置为确定。

回答by Tom01

Yes, in C global vars are initialized to zero. In Objective-C even local vars are initialized to zero. You can count on it.

是的,在 C 中全局变量被初始化为零。在 Objective-C 中,甚至局部变量也被初始化为零。你可以指望它。