Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, class instance variables are always initialized to 0 (or <code>nil</code>, <code>NULL</code>, or <code>false</code>, depending on the exact data type). See the <a href="http://web.archive.org/web/20111006112529/http://developer.apple.com/library/mac/#/web/20111006132944/http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW1" rel="noreferrer">Objective-C 2.0 Programming Language</a>:</p> <blockquote> <p>The <code>alloc</code> method dynamically allocates memory for the new object’s instance variables and initializes them all to 0—all, that is, except the <code>isa</code> variable that connects the new instance to its class.</p> </blockquote> <hr> <p><strong>EDIT 2013-05-08</strong><br> Apple seems to have removed the above document (now linked to The Wayback Machine). The (currently) active document <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html" rel="noreferrer">Programming With Objective-C</a> contains a similar citation:</p> <blockquote> <p>The <code>alloc</code> 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.</p> </blockquote> <hr> <p>However, this is <em>only</em> true for instance variables of a class; it is also true for POD types declared at global scope:</p> <pre><code>// At global scope int a_global_var; // guaranteed to be 0 NSString *a_global_string; // guaranteed to be nil </code></pre> <p>With one exception, it is <em>not</em> true for local variables, or for data allocated with <code>malloc()</code> or <code>realloc()</code>; it is true for <code>calloc()</code>, since <code>calloc()</code> explicitly zeros out the memory it allocates.</p> <p>The one exception is that when Automatic Reference Counting (ARC) is enabled, stack pointers to Objective-C objects are implicitly initialized to <code>nil</code>; however, it's still good practice to explicitly initialize them to <code>nil</code>. From the <a href="https://developer.apple.com/library/ios/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011226-CH1-SW5" rel="noreferrer">Transitioning to to ARC Release Notes</a>:</p> <blockquote> <p>Stack Variables Are Initialized with <code>nil</code></p> <p>Using ARC, strong, weak, and autoreleasing stack variables are now implicitly initialized with <code>nil</code></p> </blockquote> <p>In C++ (and C++ objects being used in Objective-C++), class instance variables are also <em>not</em> zero-initialized. You must explicitly initialize them in your constructor(s).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload