Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 28, 2011, 12:35 PM   #1
Sean Kim
macrumors newbie
 
Join Date: Jun 2011
alloc - no init - release --> no error

in this reference page..
http://developer.apple.com/library/m.../NSObject.html
it says
"You must complete the object initialization (using an init method) before invoking release."

but I tried code below, I got no errors (with -Wall compile option)

Code:
#import <Foundation/NSObject.h>
#import <stdio.h>

@interface MyObject : NSObject {
	int var;
}
-(void) set: (int) arg;
-(int) get;
@end

@implementation MyObject 
-(void) set: (int) arg {
	var = arg;
}
-(int) get {
	return var;
}
@end

int main(int argc, const char *argv[]) {
	id anObject = [MyObject alloc];
	[anObject release];
	return 0;
}
and I tried another...

Code:
#import <Foundation/NSObject.h>
#import <stdio.h>

@interface MyObject : NSObject {
	int var;
}
-(void) set: (int) arg;
-(int) get;
@end

@implementation MyObject 
-(void) set: (int) arg {
	var = arg;
}
-(int) get {
	return var;
}
@end

int main(int argc, const char *argv[]) {
	id anObject = [MyObject alloc];
	printf("addr: %p\n", anObject);
	[anObject set: 5];
	[anObject release];

	anObject = [MyObject alloc];
	printf("addr: %p\n", anObject);
	printf("var: %d\n", [anObject get]);

	return 0;
}


result:
addr: 0x100108ec0
addr: 0x100108ec0
var: 0
I thought "alloc" is just reserve memory and "init" will do something like wipe out that memory with zeros.
But result above shows that alloc zero out the memory.
What kind of work is done by NSObject's init method?

Thanks in advance~


system info:
i686-apple-darwin10-gcc-4.2.1
mac osx 10.6.7
Sean Kim is offline   0 Reply With Quote
Old Jun 28, 2011, 12:48 PM   #2
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by Sean Kim View Post
I thought "alloc" is just reserve memory and "init" will do something like wipe out that memory with zeros.
But result above shows that alloc zero out the memory.
What kind of work is done by NSObject's init method?
See The Objective-C Programming Language guide. In particular, look at the "Allocating and Initializing Objects" section:
The alloc and allocWithZone: methods initialize a newly allocated object’s isa instance variable so that it points to the object’s class (the class object). All other instance variables are set to 0. Usually, an object needs to be more specifically initialized before it can be safely used.

This initialization is the responsibility of class-specific instance methods that, by convention, begin with the abbreviation “init”. If the method takes no parameters, the method name is just those four letters, init. If it takes parameters, labels for the parameters follow the “init” prefix. For example, an NSView object can be initialized with an initWithFrame: method.
chown33 is offline   2 Reply With Quote
Old Jun 28, 2011, 04:21 PM   #3
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
In practice, you can occasionally "get away with" stuff that isn't supposed to work, due to details of the implementation. In this case, -init on NSObject doesn't actually do anything at the moment. It's easy to imagine a situation in which that could change, though, and any code relying on that fact would then break.

TLDR: -[NSObject init] does nothing, but you can't rely on that.
Catfish_Man is offline   1 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: No known class method for selector 'alloc' ? ArtOfWarfare Mac Programming 14 Jan 8, 2014 01:20 PM
Resolved: ARC, @property, alloc/init larswik iPhone/iPad Programming 5 Aug 25, 2013 05:36 PM
When to use [[alloc]init] Branda22 iPhone/iPad Programming 20 Jun 26, 2013 01:45 PM
Silly question about alloc/init for UILabel animefx iPhone/iPad Programming 1 Jul 14, 2012 12:53 AM

Forum Jump

All times are GMT -5. The time now is 02:39 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC