You are on page 1of 21

CS250 - Data Structures & Algorithms

Lecture 3: Review C++ (Continued)

Dr. Muhammad Shahzad


muhammad.shehzad@seecs.edu.pk

Department Of Computing (DOC),


School of Electrical Engineering & Computer Science (SEECS),
National University of Sciences & Technology (NUST)

21/10/2020
Today‘s lecture

▪ Pointers
▪ Referencing vs pointers
▪ Arrays & dynamic memory allocation
▪ Stack vs heap
▪ The new operator & memory leaks
▪ Concept of shalow/deep copying
▪ Void pointer

M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued) 2


Case of 1D array

cout << "Enter array size: ";


cin >> SIZE;

int *arr;
arr = new int[SIZE]; // allocation

delete [] arr; // deallocation

M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued) 3


Case of 2D arrays

cout << "Enter numRows and numCols: ";


cin >> numRows >> numCols;

int **arr2D;
arr2D = new int* [numRows]; // allocation

for(i=0; i<numRows; i++)


arr2D[i] = new int[numCols];

for(i=0; i<numRows; i++) // deallocation


delete [] arr2D[i];

delete [] arr2D;
(individual elements can be accessed using indices!! (e.g., arr2D[0][2]=10;))

M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued) 4


Stack vs Heap

▪ When a program is loaded into memory, it is organized into


three areas of memory, called segments:
► text segment (code segment / instruction)
► stack segment
► heap segment

M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued) 5


Stack vs Heap

Stack:
▪ Special region of computer's memory that stores temporary
variables created by each function, including the main()
▪ Stack variables only exist in the block of code in which they
were declared (unless declared static)

▪ Advantages:
► Memory managed automatically
► CPU organizes stack memory therefore reading from and
writing to stack variables is very fast and efficient
▪ Disdvantages:
► Size limits (depending on the OS)
► Large chunks of memory, such as very large arrays, should
not be allocated on the stack to avoid overfilling the stack
memory (known as stack overflow)
M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued) 6
Stack vs Heap

Heap:
▪ Region of your computer's memory that is NOT managed
automatically

▪ Advantages:
► More free-floating region of memory i.e., only limited by the
size of virtual memory (i.e., RAM and swap space)
► May be accessed outside of the block in which it was
allocated
▪ Disadvantage:
► Needs to be explicitly freed by the program – Memory leaks

M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued) 7


Heap vs Stack
▪ Stack ▪ Heap
▪ local variables are put on the stack ▪ global variables are on the heap
-unless they are also declared as
▪ static local variables are on the heap
'static’
(this is how they keep their value
▪ function parameters are allocated between function calls)
on the stack
▪ memory allocated by new, malloc
▪ local variables that are declared on and delete, calloc are on the heap
the stack are not automatically
▪ The heap segment provides more
initialized by the system so they
stable storage of data for a program;
usually have garbage in them until
you set them ▪ memory allocated in the heap
remains in existence for the duration
▪ variables on the stack disappear
of a program.
when the function exits (thus, if a
function is called multiple times, it's ▪ The memory allocated in the heap
local variables and parameters are area, if initialized to zero at program
recreated and destroyed each time start, remains zero until the program
the function is called and exited). makes use of it. Thus, the heap area
need not contain garbage.

M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued) 8


Classes

M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued) 9


Classes

M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued) 10


Quiz time!
#include <iostream> void main()
using namespace std; {
class A C objectC ;
{ public: A objectA;
A() B objectB;
{cout<<"In Constructor of Class A"<<endl;} }
~A()
{cout<<"In the Destructor of Class A"<<endl;}};
Output ?
class B
{ public:
B()
{cout<<"In Constructor of Class B"<<endl;}
~B()
{cout<<"In the Destructor of Class B"<<endl;}};
class C : public B, public A
{ public:
C()
{cout<<"In Constructor of Class C"<<endl;}
~C()
{cout<<"In the Destructor of Class C"<<endl;}};
M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued) 11
Quiz time!
#include <iostream> void main()
using namespace std; {
class A C objectC ;
{ public: A objectA;
A() B objectB;
{cout<<"In Constructor of Class A"<<endl;} }
~A()
{cout<<"In the Destructor of Class A"<<endl;}};
Output ?
class B
{ public:
In Constructor of Class B
B()
In Constructor of Class A
{cout<<"In Constructor of Class B"<<endl;}
In Constructor of Class C
~B()
In Constructor of Class A
{cout<<"In the Destructor of Class B"<<endl;}};
In Constructor of Class B
class C : public B, public A
In the Destructor of Class B
{ public:
In the Destructor of Class A
C()
In the Destructor of Class C
{cout<<"In Constructor of Class C"<<endl;}
In the Destructor of Class A
~C()
In the Destructor of Class B
{cout<<"In the Destructor of Class C"<<endl;}};
M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued) 12
How to design classes?

M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued) 13


Class example: A traffic light

M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued) 14


Class example: A traffic light

M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued) 15


Class example: A traffic light

M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued) 16


Shallow / Deep Copy

struct Node{
char *name; Output:
int age; => Wendy 20 Wendy 30
Node(char *n = “”, int a=0) {
name = strdup(n); The reason for this is that the
age = a; definition of Node does not
} provide a copy constructor
};
Node(const Node&);
Node node1(“Roger”, 20);
Node node2(node1); // node2 = node1;
The intention of these declarations is to create object node1, assign values to
the two data members in node1, and then create object node2 and initialize
its data members to the same values as in node1
strcpy(node2.name, “Wendy”);
node2.age = 30;
cout<<node1.name<<“ ”<<node1.age<<“ ”<<node2.name<<“ ”<<node2.age;
M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued)
Shallow / Deep Copy

▪ If a user copy constructor is missing, the constructor is


generated automatically by the compiler
► But the compiler-generated copy constructor performs
member-by-member copying; and
► since name is a pointer, the copy constructor copies the string
address node1.name to node2.name, but NOT the string
content, so that right after execution of the declaration

Node node1(“Roger”, 20); strcpy(node2.name, “Wendy”);


Node node2(node1); // or node2 = node1; node2.age = 30;

M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued) 19


Shallow / Deep Copy
Node node1(“Roger”, 20);
struct Node{ Node node2(node1); // or node2 = node1;
char *name;
int age;
Node(char * n=“”, int a=0) {
name = strdup(n);
age = a;
}
Node(const Node& n){
name = strdup(n.name); strcpy(node2.name, “Wendy”);
age = n.age; node2.age = 30;
}
};

A copy constructor is a member


function which initializes an object
using another object of the same class

M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued)


Void pointer

▪ A void* is a generic pointer meaning that pointer of any type


can be assigned to the void pointer

▪ Once assigned the type of void* is the same as that of


assigned pointer type

▪ Dereferencing a void* is a syntax error


void* sPtr;
int num;
int z[3] = {1,2,3};
sPtr = z;
num = *sPtr; // ERROR
num = *(int*) sPtr; // correct version

M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued) 21


Next lecture topic:

► Linked lists

M. Shahzad: Data Structures & Algorithms Lecture 3: Review C++ (Continued) 22

You might also like