Object C memory allocation size

Source: Internet
Author: User

Although I have been doing iOS development for some time, I have been busy with projects and have not carefully studied OC. It is rare that I have to study the OC information at leisure today and find that the effect is very good, however, I also felt that the technology update was too fast, and I found that the previous information was somewhat different from the current technology.

In terms of memory, the memory size of an object is determined by the member variables of this class when memory is allocated. For example:

@ Interface car: nsobject

{

Int year;

Nsstring * make;

Nsstring * model;

}

@ End

For this car class, the output result of the data is:

Nsobject * object = [[nsobject alloc] init];

Car * testcar = [[Car alloc] init];

Nslog (@ "% lu", sizeof (object); // 4B

Nslog (@ "% lu", sizeof (testcar); // 16b

The reason is that "the car class inherits nsobject, so there is 4B memory first, and each of the three member variables occupies 4B, that is, 4B + 3 * 4B = 16B ".

However, the output result is as follows:

Nslog (@ "% lu", sizeof (object); // 8b

Nslog (@ "% lu", sizeof (testcar); // 8b

All are 8B.


Object C memory allocation size

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.