15
Objects and Memory

In this chapter, you will learn about the life of objects on the heap and how heap memory is managed.

On pointers and their values

Objects can only be accessed via a pointer, and it is practical, if inaccurate, to refer to an object by its pointer, as in now is an NSDate. However, is important to remember that the pointer and the object that it points at are not the same thing. Here is a more accurate statement: now is a pointer that can hold an address of a location in memory where an instance of NSDate lives.

Create a new Command-line Tool named TimesTwo. Make its type Foundation. In main.m, declare a variable that points to an instance of NSDate.

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​@​a​u​t​o​r​e​l​e​a​s​e​p​o​o​l​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​c​u​r​r​e​n​t​T​i​m​e​ ​=​ ​n​i​l​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​c​u​r​r​e​n​t​T​i​m​e​'​s​ ​v​a​l​u​e​ ​i​s​ ​%​p​"​,​ ​c​u​r​r​e​n​t​T​i​m​e​)​;​
 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Here you have initialized the pointer variable to nil. Run the program, and you will find that currentTime points at 0x0 which is the value of nil.

On pointers and their values

This diagram shows the currentTime local variable that is part of the frame for main(). Its current value is nil, and there are no objects yet created on the heap.

Next, create an NSDate for currentTime to point at instead of pointing at nil.

.​.​.​
 ​ ​ ​ ​@​a​u​t​o​r​e​l​e​a​s​e​p​o​o​l​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​c​u​r​r​e​n​t​T​i​m​e​ ​=​ ​[​N​S​D​a​t​e​ ​d​a​t​e​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​c​u​r​r​e​n​t​T​i​m​e​'​s​ ​v​a​l​u​e​ ​i​s​ ​%​p​"​,​ ​c​u​r​r​e​n​t​T​i​m​e​)​;​
 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Build and run the program. The output will report the address of the object pointed to by currentTime. An NSDate object now exists on the heap.

On pointers and their values

currentTime is a variable, so you can change it to point at a different NSDate. Make the program sleep for two seconds after the first log statement and then point currentTime at a second instance of NSDate.

 ​ ​ ​ ​@​a​u​t​o​r​e​l​e​a​s​e​p​o​o​l​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​c​u​r​r​e​n​t​T​i​m​e​ ​=​ ​[​N​S​D​a​t​e​ ​d​a​t​e​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​c​u​r​r​e​n​t​T​i​m​e​'​s​ ​v​a​l​u​e​ ​i​s​ ​%​p​"​,​ ​c​u​r​r​e​n​t​T​i​m​e​)​;​

 ​ ​ ​ ​ ​ ​ ​ ​s​l​e​e​p​(​2​)​;​

 ​ ​ ​ ​ ​ ​ ​ ​c​u​r​r​e​n​t​T​i​m​e​ ​=​ ​[​N​S​D​a​t​e​ ​d​a​t​e​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​c​u​r​r​e​n​t​T​i​m​e​'​s​ ​v​a​l​u​e​ ​i​s​ ​n​o​w​ ​%​p​"​,​ ​c​u​r​r​e​n​t​T​i​m​e​)​;​
 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Build and run the program. Two seconds after the first line of output, you will see a second line reporting a different address for currentTime. currentTime now points at a different NSDate:

On pointers and their values

What about the original date object? From your code’s perspective, this object and the information it contained are lost. If you lose your only pointer to an object, then you can no longer access it – even if it continues to exist on the heap.

If you wanted to change currentTime’s value and still be able to access the original date, you could declare another pointer to store the address of the original date.

 ​ ​ ​ ​@​a​u​t​o​r​e​l​e​a​s​e​p​o​o​l​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​c​u​r​r​e​n​t​T​i​m​e​ ​=​ ​[​N​S​D​a​t​e​ ​d​a​t​e​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​c​u​r​r​e​n​t​T​i​m​e​'​s​ ​v​a​l​u​e​ ​i​s​ ​%​p​"​,​ ​c​u​r​r​e​n​t​T​i​m​e​)​;​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​s​t​a​r​t​T​i​m​e​ ​=​ ​c​u​r​r​e​n​t​T​i​m​e​;​

 ​ ​ ​ ​ ​ ​ ​ ​s​l​e​e​p​(​2​)​;​

 ​ ​ ​ ​ ​ ​ ​ ​c​u​r​r​e​n​t​T​i​m​e​ ​=​ ​[​N​S​D​a​t​e​ ​d​a​t​e​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​c​u​r​r​e​n​t​T​i​m​e​'​s​ ​v​a​l​u​e​ ​i​s​ ​n​o​w​ ​%​p​"​,​ ​c​u​r​r​e​n​t​T​i​m​e​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​T​h​e​ ​a​d​d​r​e​s​s​ ​o​f​ ​t​h​e​ ​o​r​i​g​i​n​a​l​ ​o​b​j​e​c​t​ ​i​s​ ​%​p​"​,​ ​s​t​a​r​t​T​i​m​e​)​;​
 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Build and run the program.

On pointers and their values

Now let’s look at this code progression from the perspective of memory management.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset