6

I'm learning Objectiv C, and I hear the term "live in the heap" constantly, from what I understand its some kind of unknown area that a pointer lives in, but trying to really put head around the exact term...like "we should make our property strong so it won't live in the heap. He said that since the property is private. I know it'ss a big difference It's pretty clear that we want to make sure that we want to count the reference to this object so the autorelease wont clean it (we want to "retain" it from what i know so far), but I want to make sure I understand the term since it's being use pretty often.

Appreciate it

JohnBigs
  • 2,327
  • 3
  • 25
  • 52
  • @Shashank sorry edited it, my bad (it's not my first language :/ although it's not an excuse) – JohnBigs Mar 09 '13 at 03:37
  • 1
    "we should make our property strong so it won't live in the heap" makes no sense. It would help if you pointed us to actual usage. – tc. Mar 09 '13 at 03:37
  • This question is answered best by http://stackoverflow.com/a/80113/836263 – Andrew Mar 09 '13 at 03:41
  • @tc. sorry, he said that since the property is private, I know it'ss a big difference – JohnBigs Mar 09 '13 at 03:42
  • You ever seen a teenager's bedroom? – Hot Licks Mar 09 '13 at 03:58
  • 2
    Whether or not a property is private or strong has nothing to do with how it is allocated or stored (beyond reference counting). Whoever said that has no idea what they are talking abut. – bbum Mar 09 '13 at 15:41

1 Answers1

14

There are three major memory areas used by C (and by extension, Objective C) programs for storing the data:

  • The static area
  • The automatic area (also known as "the stack"), and
  • The dynamic area (also known as "the heap").

When you allocate objects by sending their class a new or alloc message, the resultant object is allocated in the dynamic storage area, so the object is said to live in the heap. All Objective-C objects are like that (although the pointers that reference these objects may be in any of the three memory data areas). In contrast, primitive local variables and arrays "live" on the stack, while global primitive variables and arrays live in the static data storage.

Only the heap objects are reference counted, although you can allocate memory from the heap using malloc/calloc/realloc, in which case the allocation would not be reference-counted: your code would be responsible for deciding when to free the allocated dynamic memory.

Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399