Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Objective-C Basics (Retired) Beyond the Basics Alloc and Init

How compiler can understand how much memory we need to allocate before we will tell him what type we have in mind?

And how much memory will be allocated anyway?

3 Answers

James Tink
James Tink
11,210 Points

You are telling the compiler which type you have in mind when you allocate:

[NSString alloc]

Asks the NSString class to allocate enough memory to store an NSString object.

The class knows how much memory is needed based on the size of each of its members. The size of primitive type members is built in to the compiler. Any non-primitive type is just a composition of primitive types, so the compiler figures out the size of non-primitive types based on how many primitive types it has as members.

You are giving a type though when you declare the object....such as NSString *name tells it that it is an NSString object.

Almas Adilbek
Almas Adilbek
4,416 Points

I think, you don't even should understand it yourself. Just write code like conventions say.

I believe that I don't need for programming, but it will be good to know for better understanding.