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 Object-Oriented Objective-C Memory, Arrays and Loops, Oh My! Introduction to Memory and Pointers

Duplication/Redundancy

Video says that instead of full variables or classes/objects we pass around their pointers so it prevents duplication of those variables/objects. could you please elaborate...? I know C and I think once any variable or any other data structure is declared, it occupies only that much space... So how is duplication possible while dealing with them during the course of program?

2 Answers

For objects or variables , when you assign them to another variable or pass them to a function for example, object content or variable content will be copied to a whole new place in memory and assigned to a the new variable, that what causes duplication. consider this simple example:

int x = 100; if you pass x to a function like this: [self aFunction:x]

-(void) aFunction:(int)param { // do something with param } This will result that the value of x is copied to a new place in memory and assigned to variable "param" inside the function.

However when use a pointer, you make sure you only pass the address of the memory location where the object or variable sets, so no other copy of the object is created. int x = 100; int *xp = &x; //to store the memory address of x in a pointer called xp [self aFunction:x]

-(void) aFunction:(int*)param {... } // in this case param is actually holding a memory address that points to where x is stored.

Ohkey... Quite clear now... Thnx Safwat:-)