Object Oriented Programming
Pointers continued . . .
Mr. Usman Wajid
[Link]@[Link]
Usman Wajid Object Oriented Programming Pointers continued . . . 1/9
Pointer to Objects
• So far we treated objects as ordinary variables that are created when declared and
vanished when goes out of scope
Usman Wajid Object Oriented Programming Pointers continued . . . 2/9
Pointer to Objects
• So far we treated objects as ordinary variables that are created when declared and
vanished when goes out of scope
• Objects may also be created dynamically and destroyed when required
Usman Wajid Object Oriented Programming Pointers continued . . . 2/9
Pointer to Objects
• So far we treated objects as ordinary variables that are created when declared and
vanished when goes out of scope
• Objects may also be created dynamically and destroyed when required
• In other words, objects may appear on demand
Usman Wajid Object Oriented Programming Pointers continued . . . 2/9
Pointer to Objects example
• an object created
class Student { using the new
public : keyword
Student () {
cout < < " Object constructed " << endl ;
}
~ Student () {
cout < < " Object destructed " << endl ;
}
};
int main () {
Student * ptr1 = new Student ;
Student * ptr2 = new Student () ;
delete ptr1 ;
delete ptr2 ;
}
Usman Wajid Object Oriented Programming Pointers continued . . . 3/9
Pointer to Objects example
• an object created
class Student { using the new
public : keyword
Student () {
cout < < " Object constructed " << endl ; • We can omit empty
} parenthesis after the
~ Student () { class name. In either
cout < < " Object destructed " << endl ;
}
case,
}; default-constructor
int main () { will be called
Student * ptr1 = new Student ;
Student * ptr2 = new Student () ;
delete ptr1 ;
delete ptr2 ;
}
Usman Wajid Object Oriented Programming Pointers continued . . . 3/9
Pointer to Objects example
• an object created
class Student { using the new
public : keyword
Student () {
cout < < " Object constructed " << endl ; • We can omit empty
} parenthesis after the
~ Student () { class name. In either
cout < < " Object destructed " << endl ;
}
case,
}; default-constructor
int main () { will be called
• An object is
Student * ptr1 = new Student ;
Student * ptr2 = new Student () ; destroyed using the
delete ptr1 ; delete keyword
delete ptr2 ;
}
Usman Wajid Object Oriented Programming Pointers continued . . . 3/9
Pointer to Objects example
• an object created
class Student { using the new
public : keyword
Student () {
cout < < " Object constructed " << endl ; • We can omit empty
} parenthesis after the
~ Student () { class name. In either
cout < < " Object destructed " << endl ;
}
case,
}; default-constructor
int main () { will be called
• An object is
Student * ptr1 = new Student ;
Student * ptr2 = new Student () ; destroyed using the
delete ptr1 ; delete keyword
delete ptr2 ;
}
Usman Wajid Object Oriented Programming Pointers continued . . . 3/9
Pointers in fields
• All variables, including objects, are stored in separate area of memory called the stack
Usman Wajid Object Oriented Programming Pointers continued . . . 4/9
Pointers in fields
• All variables, including objects, are stored in separate area of memory called the stack
• The stacks grows when new variables are created and shrinks when the variables goes
out of scope
Usman Wajid Object Oriented Programming Pointers continued . . . 4/9
Pointers in fields
• All variables, including objects, are stored in separate area of memory called the stack
• 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
Usman Wajid Object Oriented Programming Pointers continued . . . 4/9
Pointers in fields
• All variables, including objects, are stored in separate area of memory called the stack
• 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.
Usman Wajid Object Oriented Programming Pointers continued . . . 4/9
Pointers in fields
• All variables, including objects, are stored in separate area of memory called the stack
• 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
Usman Wajid Object Oriented Programming Pointers continued . . . 4/9
Pointers in fields
• All variables, including objects, are stored in separate area of memory called the stack
• 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
Usman Wajid Object Oriented Programming Pointers continued . . . 4/9
Pointers in fields
• All variables, including objects, are stored in separate area of memory called the stack
• 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 () {
Student * ptr1 = new Student (1) ;
cout < < " rollNo : " << ptr1 - > getRollNo () << endl ;
cout < < " rollNo : " < <(* ptr1 ) . getRollNo () << endl ;
delete ptr1 ;
}
Usman Wajid Object Oriented Programming Pointers continued . . . 5/9
Pointer to Fields example
class Student {
private :
int rollNo ;
public :
Student ( int rollNo =0) { • (*p).getRollNo(); // p
setRollNo ( rollNo ) ; is explicitly
}
void setRollNo ( int rollNo ) { dereferenced
this - > rollNo = rollNo ;
}
int getRollNo () {
return rollNo ;
}
};
int main () {
Student * ptr1 = new Student (1) ;
cout < < " rollNo : " << ptr1 - > getRollNo () << endl ;
cout < < " rollNo : " < <(* ptr1 ) . getRollNo () << endl ;
delete ptr1 ;
}
Usman Wajid Object Oriented Programming Pointers continued . . . 5/9
Pointer to Fields example
class Student {
private :
int rollNo ;
public :
Student ( int rollNo =0) { • (*p).getRollNo(); // p
setRollNo ( rollNo ) ; is explicitly
}
void setRollNo ( int rollNo ) { dereferenced
this - > rollNo = rollNo ;
} • p-> getRollNo(); // p
int getRollNo () {
return rollNo ;
is implicitly
} dereferenced
};
int main () {
Student * ptr1 = new Student (1) ;
cout < < " rollNo : " << ptr1 - > getRollNo () << endl ;
cout < < " rollNo : " < <(* ptr1 ) . getRollNo () << endl ;
delete ptr1 ;
}
Usman Wajid Object Oriented Programming Pointers continued . . . 5/9
Pointer to Fields example
class Student {
private :
int rollNo ;
public :
Student ( int rollNo =0) { • (*p).getRollNo(); // p
setRollNo ( rollNo ) ; is explicitly
}
void setRollNo ( int rollNo ) { dereferenced
this - > rollNo = rollNo ;
} • p-> getRollNo(); // p
int getRollNo () {
return rollNo ;
is implicitly
} dereferenced
};
int main () { • Output:
Student * ptr1 = new Student (1) ;
cout < < " rollNo : " << ptr1 - > getRollNo () << endl ;
cout < < " rollNo : " < <(* ptr1 ) . getRollNo () << endl ;
delete ptr1 ;
}
Usman Wajid Object Oriented Programming Pointers continued . . . 5/9
Memory Leaks
• Entities such a variable and objects are allocated memory to perform their operations
Usman Wajid Object Oriented Programming Pointers continued . . . 6/9
Memory Leaks
• 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
Usman Wajid Object Oriented Programming Pointers continued . . . 6/9
Memory Leaks
• 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
Usman Wajid Object Oriented Programming Pointers continued . . . 6/9
Memory Leaks Example
class Section {
public :
int * totalStudents ;
Section ( int num ) {
totalStudents = new int [ num ];
}
~ Section () {
cout < < " Object destructed " << endl ;
}
};
void makeALeak () {
Section secA (50) ;
}
int main () {
makeALeak () ;
}
Usman Wajid Object Oriented Programming Pointers continued . . . 7/9
Memory Leaks Example
class Section {
public :
int * totalStudents ;
Section ( int num ) {
totalStudents = new int [ num ];
}
~ Section () {
cout < < " Object destructed " << endl ;
}
};
void makeALeak () {
Section secA (50) ;
}
int main () {
makeALeak () ;
}
Usman Wajid Object Oriented Programming Pointers continued . . . 7/9
Memory Leaks continued . . .
• The constructor allocates another part of memory (heap) to pointer totalStudents
Usman Wajid Object Oriented Programming Pointers continued . . . 8/9
Memory Leaks continued . . .
• The constructor allocates another part of memory (heap) to pointer totalStudents
• The object ”secA” is an example of automatic variable, i.e., it is removed from memory
when it goes out of scope
Usman Wajid Object Oriented Programming Pointers continued . . . 8/9
Memory Leaks continued . . .
• The constructor allocates another part of memory (heap) to pointer totalStudents
• 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
Usman Wajid Object Oriented Programming Pointers continued . . . 8/9
Memory Leaks continued . . .
• The constructor allocates another part of memory (heap) to pointer totalStudents
• 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
Usman Wajid Object Oriented Programming Pointers continued . . . 8/9
Memory Leaks continued . . .
• The constructor allocates another part of memory (heap) to pointer totalStudents
• 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)
Usman Wajid Object Oriented Programming Pointers continued . . . 8/9
Memory Leaks Example
class Section {
public :
int * totalStudents ;
• Solution:
Section ( int num ) {
totalStudents = new int [ num ]; Put the necessary code in the
} destructor to ensure that
~ Section () { when object secA goes out of
delete [] totalStudents ; scope, the memory allocated
cout < < " Object destructed " << endl ;
}
to the dynamic constructor
}; totalStudents is retrieved
or freed
void makeALeak () {
Section secA (50) ;
}
int main () {
makeALeak () ;
}
Usman Wajid Object Oriented Programming Pointers continued . . . 9/9
Memory Leaks Example
class Section {
public :
int * totalStudents ;
• Solution:
Section ( int num ) {
totalStudents = new int [ num ]; Put the necessary code in the
} destructor to ensure that
~ Section () { when object secA goes out of
delete [] totalStudents ; scope, the memory allocated
cout < < " Object destructed " << endl ;
}
to the dynamic constructor
}; totalStudents is retrieved
or freed
void makeALeak () {
Section secA (50) ;
}
• Output:
int main () {
makeALeak () ;
}
Usman Wajid Object Oriented Programming Pointers continued . . . 9/9