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 trialJoseph Lau
5,420 PointsObjective-C: Dynamic Memory Allocation?
@7:30 of the video:
1) He instantiates a "TYPE" Sphere called ball; is this what's called an "instance of a class" also known as "object"? Or is Sphere simply analogous to a data "TYPE" like int, float, and char? Or even perhaps it means both in which a "class" means "a data type which groups together other data types and methods", such as a "struct" in C?
2) Also, at the same time in the video, what does he mean when he says "we need to ALLOCATE it because it's using DYNAMIC MEMORY"? What does allocating an object even mean? Dynamic memory?
1 Answer
Stone Preston
42,016 Pointsyes he creates a Sphere object named ball. Sphere is a class, which means that instances of that class are called objects. A Sphere is not a primitive type like int, float etc. It is an object.
From Wikipedia on Dynamic memory in objective-C:
^
In Objective-C (unlike in C++), all objects are created dynamically. This means that they continue to exist until they are explicitly destroyed.
To create an object, send its class an alloc message: [aClass alloc];. Note that this returns a new instance of the class but does not initialize its data. Typically you want to immediately send the new object an init message to remedy this. Allocation and initialization are usually combined in one line:
aClass *newobj = [[aClass alloc] init];
http://en.wikibooks.org/wiki/Cocoa_Programming/Memory_management