Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "live in the heap" mean?

Tags:

objective-c

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

like image 253
JohnBigs Avatar asked Mar 09 '13 03:03

JohnBigs


1 Answers

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.

like image 141
Sergey Kalinichenko Avatar answered Oct 13 '22 11:10

Sergey Kalinichenko