Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Can someone explain what the compiler does when it sees this?</p> </blockquote> <pre><code> DooDad* aDooDad = nil; </code></pre> <p>If you are really interested in what the compiler does, the answer is: the compiler will reserve some memory on the stack for the local variable <code>aDooDad</code>, which is a pointer type (it is generally 64 or 32 bits in size depending on the processor). That pointer is then initialized to contain <code>nil</code> (usually <code>0x00..00</code>).</p> <p>A statement like this:</p> <pre><code> DooDad* aDooDad = [[DooDad alloc] init...]; </code></pre> <p>makes use of pointer variable <code>aDooDad</code> to store the address in memory of the object that is further allocated (which is the address of memory reserved by <code>alloc</code>).</p> <p>So, in the end,</p> <pre><code> DooDad* aDooDad = nil; </code></pre> <p>is not declaring an object, just a variable whose content is interpreted as the address of an object of <code>DooDad</code> type. Such declaration, therefore, is just like any other declaration you know, e.g. when initializing an <code>int</code> to 0, so that later you can assign it some value in an <code>if</code> statement.</p> <p>A statement like:</p> <pre><code> [aDooDad doSomething]; </code></pre> <p>is interpreted by the Objective-C runtime system like: send message <code>doSomething</code> to the object whose address is stored in <code>aDooDad</code>. If that address is <code>nil</code> no message is sent. On the other hand, if you <em>dereference</em> a nil pointer: <code>*aDooDad</code> you'll get undefined behavior.</p> <p>Pointers are pretty low level stuff. I hope this helps.</p>
 

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