Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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.