Junior — Middle
Where are pointers or references to objects usually stored in a program?
sobes.tech AI
Answer from AI
In iOS applications, object pointers or references are usually stored on the stack or heap, depending on context. Local variables and references to objects declared inside functions or methods are stored on the stack. If an object is created dynamically (e.g., via alloc/init or ARC), the object itself is stored on the heap, and the reference variable points to it, stored on the stack or in the class instance area.
Additionally, in classes, references to other objects are usually stored in the class's fields (properties), which are stored on the heap along with the object itself. Thus, object pointers or references are typically stored:
- On the stack — for local variables and function parameters.
- On the heap — for object fields and dynamically allocated data.
Example in Swift:
class Person {
var name: String
init(name: String) {
self.name = name
}
}
func createPerson() {
let person = Person(name: "Ivan") // 'person' — reference on stack, object on heap
print(person.name)
}