Victor Ng's Radio Weblog : armed with cocoa, an ibook and too much spare time
Updated: 4/4/03; 12:24:35 AM.

 

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.


dotMac


Categories

Cocoa


Friends


Site Meter
 
 

Friday, January 3, 2003

On the previous topic of uninitialized pointers in methods in Objective-C - GCC3 will actually give you warnings on those variables if you've got your targets setup as 'deployment' instead of 'development'.

Apparently GCC3 disables (at least) the uninitialized variable warnings when you've got -O0 (no optimisations) set. Any other optimisation value and the compiler properly gives you warnings.

Don't ask me why - but I tried it and it seems to work.

The relevant flag is -Wuninitialized. I believe it's on by default, but GCC 3 ignores it if the optimizer is off (i.e. you are doing a Development build).

6:37:16 PM    

Pointers in Cocoa

I don’t quite understand what Victor Ng is on about. Objective-C ivar pointers are initialized to nil, and you should get a compiler warning if you attempt to use an uninitialized local variable.

[Michael Tsai[base ']s Weblog]

Mike sets me straight on Objective-C's initialization rules - sort've.

Suppose I have a class that is defined like this:

@interface VTNMyClass {
NSDate* aDate;
}
-(void)someMethod;

It turns out that when you allocate the object instance, the ObjC runtime will initialize all of your instance variables to 0 or nil.

To quote:

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.

So what the heck was going on with my stuff? Here's my test case:

-(void)testIdInitTest
{
id obj;
[self assertTrue:(obj == nil) message:@"obj should be nil if pointers are initialized"];
}

.. and it always fails.

What's going on here is that (as the doc's say) the 'alloc' method is the thing doing the initialization. Since we never alloc an object, the 'obj' pointer is never initialized to nil.

Annoying - but true.

Regardless - thanks to Michael for pointing out my errors.
5:09:17 PM    


© Copyright 2003 Victor Ng.



Click here to visit the Radio UserLand website.

 


January 2003
Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
Dec   Feb


AIM: vng1@mac.com
MSN: v2ng@hotmail.com
ICQ: 30390174

NetNewsWire: More news, less junk. Faster