Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-05-2011, 09:58 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 1
JooJar is on a distinguished road
Default Stack and Heap ... oh and Pointers.

Hi guys,

I've been reading up on the stack and heap as I see it mentioned a lot in videos. I'm just not sure if my thinking is right.

To my understanding the stack is used to push and pop functions on and off the stack. Each function as it's encountered is given a stack frame and inside the stack frame is the functions variables. When the function returns, the function is popped off the stack ... is that correct?

The heap is used to store variables and objects at run time. In Objective C a class is also an object so it's stored on the heap. Memory segments are requested from the heap using alloc/malloc and returned using free (I believe it's release in obj-c). Am o the right lines?

I believe everything else such as globals, constants etc are stored in the data segment of memory when the program is ran. Might be wrong here.

I've watched 2 hour long videos on it from Stanford because it previous to learning confused me. I done it at university but have totally forgot since then.

One final thing, pointers.

Is the purpose of pointers used to reference memory locations so that memory is not wasted and memory locations can be easily accessed and modified?

Whilst I know how to use pointers and the syntax is no longer trivial, I failed to understand their exact purpose. Books don't do a good job of explaining their purpose apart from how to access and traverse arrays with them :/

Thanks.

Last edited by JooJar; 03-05-2011 at 10:01 AM. Reason: Fixed some typos.
JooJar is offline   Reply With Quote
Old 03-05-2011, 10:19 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Your stack/heap understanding seems good; but to explain pointers I'll have to start with a fairy tale about ***A NIGHTMARE WORLD WITH NO POINTERS***.

If pointers did not exist then you would be forced to make copies of objects every time you added them to arrays or passed them into methods. For example, suppose you had an array of "Person" objects and you wanted to change all of their middle names to "Bubba." You would have to do something like this:

Code:
for(int i=0; i<[peopleArray count]; i++){
     Person person = [peopleArray objectAtIndex:i]; //copy the person
     person.middleName = @"Bubba"; //copy the Bubba string into each object!
     [peopleArray replaceObjectAtIndex:i withObject:person]; //copy him back
}
All that copying would be terribly slow! What if we lived in a better world, where you didn't have to copy an object to add it to an array? Suppose the array could just get the address of the original object and keep track of the object that way? That's what a pointer is, the address of an object*. I could also access the array by getting a pointer to the object inside the array.

Luckily this is the world we live it; Objective-C uses pointer all over the place, and our code can get much simpler:
Code:
for(int i=0; i<[peopleArray count]; i++){
     Person *person = [peopleArray objectAtIndex:i]; //get a pointer, not a copy
     person.middleName = @"Bubba"; //point the middleName pointers at the same Bubba string 
     //no need to copy back; I modified the original object right inside the array.
In our world of pointers programs can be shorter and use less memory and run much faster because there is less copying going on.

*Pointers are not just used with objects -- you can have pointers to integers and other primatives -- but it's not as common in Objective-C.
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 484
15 members and 469 guests
7twenty7, AlanFloyd, AppsBlogger, David-T, iAppDeveloper, imac74, Jaxen66, lovoyl, Music Man, mutantskin, Paul Slocum, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:13 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0