BMITime instance creation question


Hi, I have question about BMI Time sample on this chapter.

On main.m, book has following code for creating instance for employee array

for (int i = 0; i < 10; i++)
            // Create an instance of Employee
            Employee *person = [[Employee alloc] init];
            // Give the instance variable interesting value
            [person setWeightInKilos:90 + i];
            [person setHeightInMeters:1.80 + i/10];
            [person setEmployeeID:i];
            // Put the employee in employee array
            [employee addObject:person];

Being a novice programming student, I was having tough time understanding this chapter, so I broke the for-loop into manual array insertion like this:

Employee *person1 = [[Employee alloc] init];
[person1 setWeightInKilos:90];
[person1 setHeightInMeters:1.8];
[person1 setEmployeeID:1];

On for-loop method, employee instance is declared as “person” and it is used multiple times to create Employee array object. On non-loop method, for each employee data, I created a new instance with different name (perons1, person2, person3…). But loop method only uses one instance (person) to create 10 employee data in array.

My question is, when looping finished each iteration, does system updates(overwrites) ‘person’ instance data with new data or creates a new one like non-loop method, but somehow automatically knows new ‘person’ instance’s memory address?

My current logic tell me that, when loop is used instance get created > data is passed to array object > wipes the old data > start new iteration with new data. But I know this is a flawed logic…

Thank you!


I think I understand the question. If this answer is off topic, please let me know.

The important point is that person is a pointer. The first time through the loop it holds the address of the first Employee object. The second time through the loop it holds the address of the second Employee object. So, yes, we reuse it – each time it points to a different address in memory.

Before we give the person variable a new address, we store the old address in an array.

When you create 10 variables (person1, person2, etc.) you have a different variable for each address. That works too.


This was the answer I was looking for. So just to clarify, even though pointer name is repeated in the loop, actual address is different correct?

Thank you for your answer Aaron!


Yes, your clarification is correct. As with any variable, the value contained by it can change (and in this case does change).