2

Suppose we declare a class as follows in MyClass.h file as follows.

#import <Foundation/Foundation.h>

@interface MyClass : NSObject


+(void)aClassMethod;

@end

And in MyClass.m File..

@implementation MyClass

+(void)aClassMethod{

NSLog(@"It is a Class method");

}
@end

My question is that after compilation where this aClassMethod will be stored?? and if we declare some member functions , then where they will be stored.

Mak083
  • 1,160
  • 1
  • 13
  • 33
  • maybe this answers your question: http://stackoverflow.com/questions/982116/objective-c-message-dispatch-mechanism or if you need more depth: http://www.friday.com/bbum/2009/12/18/objc_msgsend-part-1-the-road-map/ – vikingosegundo Dec 19 '13 at 14:24
  • Actually I wanted to know that where class method is stored in stack , code or data segment?? And what tasks are done bye the compiler to handle the class methods? – Mak083 Dec 19 '13 at 14:46
  • you should alter your question. – vikingosegundo Dec 19 '13 at 14:52
  • What you referred to as a member function is called an *instance method* in Objective-C. – jlehr Dec 19 '13 at 15:07
  • By the way, Objective-C class methods are not analogous to static methods in other languages. – jlehr Dec 19 '13 at 15:11
  • 1
    @MohanChaudhari To expand a bit on jlehr's answer, if you `clang -rewrite-objc main.c` your program, you'll see class methods turn to static C functions, so they are marked as such in the generated object. The details of equivalent C questions about memory layout apply to Objective-C, since the difference is that an Objective-C program is linked with a runtime that executes the message dispatch as indicated above by vikingosegundo. – Jano Dec 19 '13 at 15:13

2 Answers2

3

Objective-C methods are compiled into C functions, which are stored in the program's Text segment along with any other C functions the program defines. That's true for both class and instance methods.

jlehr
  • 15,557
  • 5
  • 43
  • 45
3

Actually I wanted to know that where class method is stored in stack , code or data segment?? And what tasks are done bye the compiler to handle the class methods?

This question doesn't fully make sense.

The executable code is stored where all executable code is stored; in the mach-o TEXT segments. The code is mapped into memory on readonly, executable, pages of memory. The memory cannot be made readwrite (i.e. you can't edit the executable in memory).

Objective-C methods are standard C functions that always take two arguments; self and _cmd.

In terms of calling convention, the code is called like any other C function. The only difference being the method dispatch mechanism that matches the call site ([NSObject new]) to the executable code to be invoked. That matching is done through the Objective-C messenger and is implemented by objc_msgSend() or some variant.

bbum
  • 162,346
  • 23
  • 271
  • 359