Pointers Part 2
Pointers Part 2
Pointers continued . . .
• So far we treated objects as ordinary variables that are created when declared and
vanished when goes out of scope
• So far we treated objects as ordinary variables that are created when declared and
vanished when goes out of scope
• So far we treated objects as ordinary variables that are created when declared and
vanished when goes out of scope
• The stacks grows when new variables are created and shrinks when the variables goes
out of scope
• The stacks grows when new variables are created and shrinks when the variables goes
out of scope
• Note that this process is beyond your control. You can not affect the way in which the
stack changes
• The stacks grows when new variables are created and shrinks when the variables goes
out of scope
• Note that this process is beyond your control. You can not affect the way in which the
stack changes
• The entities created ”on demand” by the new keyword are created in specific memory
region usually called a heap.
• The stacks grows when new variables are created and shrinks when the variables goes
out of scope
• Note that this process is beyond your control. You can not affect the way in which the
stack changes
• The entities created ”on demand” by the new keyword are created in specific memory
region usually called a heap.
• Unlike stack, the heap can be controlled manually
• The stacks grows when new variables are created and shrinks when the variables goes
out of scope
• Note that this process is beyond your control. You can not affect the way in which the
stack changes
• The entities created ”on demand” by the new keyword are created in specific memory
region usually called a heap.
• Unlike stack, the heap can be controlled manually
• an ordinary dot ’.’ operation can not be directly performed for entities stored in heap,
unless the pointer is deferenced
• The stacks grows when new variables are created and shrinks when the variables goes
out of scope
• Note that this process is beyond your control. You can not affect the way in which the
stack changes
• The entities created ”on demand” by the new keyword are created in specific memory
region usually called a heap.
• Unlike stack, the heap can be controlled manually
• an ordinary dot ’.’ operation can not be directly performed for entities stored in heap,
unless the pointer is deferenced
• or one can use the ”arrow” (->) operator instead
Usman Wajid Object Oriented Programming Pointers continued . . . 4/9
Pointer to Fields example
class Student {
private :
int rollNo ;
public :
Student ( int rollNo =0) {
setRollNo ( rollNo ) ;
}
void setRollNo ( int rollNo ) {
this - > rollNo = rollNo ;
}
int getRollNo () {
return rollNo ;
}
};
int main () {
• Entities such a variable and objects are allocated memory to perform their operations
• Entities such a variable and objects are allocated memory to perform their operations
• This memory should be released when these operations are done. In most cases it is
done automatically
• Entities such a variable and objects are allocated memory to perform their operations
• This memory should be released when these operations are done. In most cases it is
done automatically
• Failure to clean memory activates a phenomena known as memory leaking, i.e, the
un-accessed data residing in memory affects system performance
~ Section () {
cout < < " Object destructed " << endl ;
}
};
void makeALeak () {
Section secA (50) ;
}
int main () {
makeALeak () ;
}
~ Section () {
cout < < " Object destructed " << endl ;
}
};
void makeALeak () {
Section secA (50) ;
}
int main () {
makeALeak () ;
}
• The object ”secA” is an example of automatic variable, i.e., it is removed from memory
when it goes out of scope
• The object ”secA” is an example of automatic variable, i.e., it is removed from memory
when it goes out of scope
• On a return from the makeALeak() function, the memory allocated (stack) to secA will
be retrieved automatically
• The object ”secA” is an example of automatic variable, i.e., it is removed from memory
when it goes out of scope
• On a return from the makeALeak() function, the memory allocated (stack) to secA will
be retrieved automatically
• Unfortunately, the memory allocated to the dynamic pointer ”totalStudents” stored in
another location (heap) still resides in memory
• The object ”secA” is an example of automatic variable, i.e., it is removed from memory
when it goes out of scope
• On a return from the makeALeak() function, the memory allocated (stack) to secA will
be retrieved automatically
• Unfortunately, the memory allocated to the dynamic pointer ”totalStudents” stored in
another location (heap) still resides in memory
• Hence, a fairly large portion of memory is leaked (still resides in memory but not
accessible any more)
int main () {
makeALeak () ;
}
int main () {
makeALeak () ;
}