Black Horse programmer OC Self pointer

Source: Internet
Author: User

When an object method is called, the compiler will pass in a pointer to this object by default. So different objects will be called to the correct member variable. This pointer is self, and its value is the first address of the memory allocated in the heap when new. So is this self stored in the stack when the method is called? On the code debugging look at

#import <Foundation/Foundation.h> @interface person:nsobject{    int _age;} -(void) Setage: (int) age;-(int) age;-(void) test: (int) age; @endint Main (int argc, const char * argv[]) {person        *p = [Per Son new];        NSLog (@ "p =%p", p);        [P setage:10];        [P test:20];        return 0;} @implementation person-(void) Setage: (int) age{    _age = age;} -(int) age{    return _age;} -(void) test: (int) age{    int _age =;    NSLog (@ "local variable _age =%d", _age);    NSLog (@ "Age:%d", self->_age);} @end

The value in self is observed in the same way as the value in p by the address value in the output P and the debug window. The first address of the memory space in the heap where self points to an object


When running to _age=age, the statement is broken. Observing disassembly code and related register values we found
The value in the register RSI is the same as the value in self.  EdX = 10 (i.e. low 32 bits of the RDX register); RSI = 8;

Movl%edx, (%rsi,%rdi)//This Assembly statement means to copy the values in edx to the memory pointed to by the RSI + RDI address. and the operation data length is 4 bytes.

Executes a single assembly statement one step down. Broken in Popq%RBP place. At this time Movl%edx, (%rsi,%rdi) has been executed. The statement that the breakpoint points to is a statement that will be executed but not executed
Open the Memory window pointed to by self to see again


We found that the 4-byte space at the Self + 8 address has been assigned a value of 0 A; So _age is eventually set to 10; that is, the address of the member variable begins at self+8.

What about these 8 bytes?

These 8 bytes are the ISA pointers. NSObject.h in the header file

@interface nsobject <NSObject> {    Class isa  objc_isa_availability;}
and the class is declared in Objc.h.
typedef struct OBJC_CLASS *class;

all classes in OC inherit from the NSObject class, so you naturally have the ISA pointer. Then there is the member variable that we declared in our class.

As seen above, the self parameter passed in when invoking an object method is stored in the CPU register


Black Horse programmer OC Self pointer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.