0% found this document useful (0 votes)
55 views25 pages

Lecture - 2 - ReviewC++ - PointersAndDynamicMemoryAllocation - Ahsan

This document provides a summary of Lecture 2 of the CS250 - Data Structures & Algorithms course. The lecture covers pointers and dynamic memory allocation in C++, including pointers, referencing vs pointers, arrays, stack vs heap memory, and the new operator. It provides examples and explanations of pointers, dereferencing pointers, reference variables, and using pointers to access elements in arrays and strings.

Uploaded by

Ramish Saeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views25 pages

Lecture - 2 - ReviewC++ - PointersAndDynamicMemoryAllocation - Ahsan

This document provides a summary of Lecture 2 of the CS250 - Data Structures & Algorithms course. The lecture covers pointers and dynamic memory allocation in C++, including pointers, referencing vs pointers, arrays, stack vs heap memory, and the new operator. It provides examples and explanations of pointers, dereferencing pointers, reference variables, and using pointers to access elements in arrays and strings.

Uploaded by

Ramish Saeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

CS250 - Data Structures & Algorithms

Lecture 2: Review C++ (Pointers & Dynamic


Memory Allocation)
Dr. Ahsan Saadat
[email protected]

Assistant Professor
Department Of Computing (DOC),
School of Electrical Engineering & Computer Science (SEECS),
National University of Sciences & Technology (NUST)
Recap slide

▪ Data Structure is a systematic way to organize data in order


to use it efficiently

▪ An algorithm is an effective method for solving a problem


using a finite sequence of instructions

▪ Data types:
► Primitive Data Types
- Bool, Int, float etc.
► User-Defined Data Types (UDTs)
- Aggregate data types e.g., structures, array-of-integers etc.
► Abstract Data Types (ADTs)
- Does not specify how the data type is implemented
- In an object-oriented language such as C++, an ADT and its
implementation together make up a class
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 2
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

Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 3
Pointers

Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 4
Pointers

Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 5
Pointers

Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 6
Pointers

Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 7
Pointers

Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 8
Pointers

Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 9
What is a pointer variable?

▪ A pointer variable is a variable whose value is the address


of a location in memory.

▪ To declare a pointer variable, you must specify the type of


value that the pointer will point to, for example,

int* ptr; // ptr will hold the address of an int

char* q; // q will hold the address of a char

10
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation)
Using a Pointer Variable

2000
int x;
x = 12; 12
x

int* ptr; 3000


ptr = &x; 2000
ptr

NOTE: Because ptr holds the address of x,


we say that ptr “points to” x
11
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation)
*: dereference operator

int x; 2000

x = 12; 12
x

3000
int* ptr;
2000
ptr = &x;
ptr
cout << *ptr;

NOTE: The value pointed to by ptr is denoted by *ptr


12
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation)
Using the Dereference Operator

int x; 2000

x = 12; 12 5
x

3000
int* ptr;
2000
ptr = &x; ptr

*ptr = 5; // changes the value at the


address ptr points to 5
13
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation)
Self –Test on Pointers

4000
char ch;
ch = ‘A’; A Z
ch

char* q; 5000 6000

q = &ch; 4000 4000


q p
*q = ‘Z’;
char* p;
p = q;
// the rhs has value 4000

// now p and q both point to 14ch


Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation)
Using a Pointer to Access the
Elements of a String
char msg[ ]
=“Hello”; msg
char* ptr; 3000

ptr = msg; ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’


*ptr = ‘M’ ; ‘M’ ‘a’
ptr++;

*ptr = ‘a’; 3000


3001

ptr

15
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation)
Reference Variables

Reference variable = alias for another variable


- Contains the address of a variable (like a pointer)
- No need to perform any dereferencing (unlike a pointer)
- Must be initialized when it is declared

int x = 5;
int &z = x; // z is another name for x
int &y ; //Error: reference must be initialized
cout << x << endl; -> prints 5
cout << z << endl; -> prints 5

z = 9; // same as x = 9;

cout << x << endl; -> prints 9


cout << z << endl; -> prints 9
16
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation)
Why Reference Variables?

▪Are primarily used as function


parameters

▪Advantages of using references:


► you don’t have to pass the address of a variable
► you don’t have to dereference the variable inside the called function

17
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation)
Reference Variables Example

#include <iostream.h>
void p_swap(int *a, int *b)
{
// Function prototypes
(required in C++) int temp;
temp = *a; (2)
*a = *b; (3)
void p_swap(int *, int *);
*b = temp;
void r_swap(int&, int&);
}
int main (void){
void r_swap(int &a, int &b)
int v = 5, x = 10; {
cout << v << x << endl; int temp;
p_swap(&v,&x); temp = a; (2)
cout << v << x << endl; a = b; (3)
r_swap(v,x); b = temp;
cout << v << x << endl; }
return 0;
18
}
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation)
Pointer example

Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 19
Pointer example

Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 20
Differences between references & pointers

Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 21
Differences between references & pointers

▪ Consider the following declarations:


int n=5, *p = &n, &r = n;

▪ A reference variable must be initialized in its declaration as a


reference to a particular variable, and this reference cannot be
changed, meaning that a reference variable CANNOT be null

▪ A reference variable r can be considered a different name for a


variable n
► If n changes then r changes as well. This is because a reference
variable is implemented as a constant pointer to the variable

▪ cout << n << ' ' << *p << ' ' << r << endl;
*p = 9;?
Output: 5 5 5 r = 10;?
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 22
Constant pointer vs pointer constant

▪ The declaration int& r = n; actually can be replaced by


int *const r = &n;
where r is a constant pointer to an integer, which means that
the assignment r = q; where q is another pointer, is an error
because the value of r cannot change
▪ However, the assignment *r = 1; is acceptable if n is NOT a
constant integer
▪ It is important to note the difference between the
type int *const and the type const int *
where const int * is a type of pointer to a constant integer:
const int * s = &m;
after which the assignment s = &m; where m is an integer
(whether constant or not) is admissible, but the assignment
*s = 2; is erroneous, even if m is not a constant
Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 23
Array names as pointers

#include <iostream>

void main() {
const SIZE = 5

int i, arr[SIZE] = {98, 87, 92, 79, 85};

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


cout << arr[i] << *(arr + i) << endl;
}

Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 24
Take Home

Try All The Codes In Your Own Time

Data Structures & Algorithms Lecture 2: Review C++ (Pointers and dynamic memory allocation) 25

You might also like