4 Lecture DSOOP DMAandRecursion DrTahirNawaz
4 Lecture DSOOP DMAandRecursion DrTahirNawaz
Oriented Programming
Dr Tahir Nawaz
Website: https://www.tahirnawaz.com
Email: [email protected]
Dynamic Memory Allocation
Introduction
• A good understanding of dynamic memory allocation in
C++ is essential to become a good programmer.
Memory for a C++ program is divided into two parts:
Global
delete pvalue;
int main () {
double* pvalue = NULL; // Pointer initialized with null
pvalue = new double; // Request memory for the variable
return 0;
}
Dynamic memory allocation for Arrays
• Suppose we want to allocate memory for an array of
characters, i.e., string of 20 characters. Using the same
syntax what we have used above we can allocate
memory dynamically as shown below.
#include <iostream>
using namespace std;
class Box {
public:
Box() {cout << "Constructor called!" <<endl;}
~Box() {cout << "Destructor called!" <<endl;}
};
int main() {
Box* myBoxArray = new Box[4];
delete [] myBoxArray; // Delete array
return 0; }
Dynamic memory allocation for Objects
• In the example we have allocated memory for an array of
four Box objects. So the constructor would be called four
times and similarly while deleting these objects, destructor
will also be called same number of times.
recurse()
{ Recursive
... .. ... Call
recurse();
... .. ...
}
int main()
{
... .. ...
recurse();
Function
... .. ...
Call
}
How does it work? (contd.)
• Will the following program continue forever?
#include <iostream>
using namespace std;
int main()
{
recurse ( 1 ); //First function call
}
How does it work? (contd.)
• In the previous program, computer keeps function calls
on a stack and once too many are called without ending,
the program will crash.
• This simple program shows the number of times the
recurse function has been called by initializing each
individual function call's count variable one greater than
it was previous by passing in count + 1.
• Actually, it is not a function restarting itself, it is hundreds
of functions that are each unfinished with the last one
calling a new recurse function.
• So recursion normally needs a condition where the
function will not call itself (and therefore finally exit!) and
is termed the ‘base case’ of the function.
How does it work? (contd.)
• Following function has got a base condition! Tell me the
output of this program!
#include <iostream>
using namespace std;
int main()
{
printnum(1);
}
How does it work? (contd.)
• Lets work out the output!
Function Call Recursive Call 1 Recursive Call 2
• Output?
123321
Tail vs. Head Recursion
• Tail Recursion: Some work is performed and then the
recursive call is made
• Disadvantages
– More stack space consumed than an equivalent iterative
program
– Higher processing time
– Error debugging is more difficult as compared to the iterative
program.
Acknowledgements/References
• https://www.tutorialspoint.com/cplusplus/index.htm
• https://www.cplusplus.com/
• https://www.cprogramming.com/
• https://www.youtube.com/watch?v=_8-ht2AKyH4