0% found this document useful (0 votes)
2 views

Data Structure Lecture 5

This document covers pointers in C++, including their declaration, initialization, and operations. It explains memory allocation using the 'new' operator, memory deallocation with 'delete', and the concept of dynamic arrays. Additionally, it discusses avoiding memory leaks and the importance of managing pointer variables to prevent dangling pointers.

Uploaded by

Karim Saleh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Data Structure Lecture 5

This document covers pointers in C++, including their declaration, initialization, and operations. It explains memory allocation using the 'new' operator, memory deallocation with 'delete', and the concept of dynamic arrays. Additionally, it discusses avoiding memory leaks and the importance of managing pointer variables to prevent dangling pointers.

Uploaded by

Karim Saleh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Data Structure and

Algorithms
Chapter 3
Lecture 3
• Pointer variable: A variable whose content is an
address (that is, a memory address).
• dataType *identifier;
Example
int *p;
char *ch;
 int* p, q;
In this statement, only p is a pointer variable, not
q.
The Pointer  int *p, *q;
declares both p and q to be pointer variables of
type int.
In C++, the ampersand, &, called the address of
operator
int x;
int *p;
p = &x;
assigns the address of x to p. That is, x and the
value of p refer to the same memory location.
Example 1
int *p;
int num;
num = 78;
p = #
*p = 24;
Pointers and Classes
string *str;
str = new string;
*str = "Sunny Day";

The syntax for accessing a class (struct) member using the operator -> is as follows:
pointerVariableName->classMemberName

Thus, the expression

(*str).length()
is equivalent to the expression

str->length()

Initializing Pointer Variables


p = NULL;
p = 0;
Operator new
 The operator new has two forms: one to allocate a single variable, and
another to allocate an array of variables.

 The syntax to use the operator new is as follows:


new dataType; //to allocate a single variable
new dataType[intExp]; //to allocate an array of variables
void boxType::print() const
{
 where intExp is any expression evaluating to a positive integer.
rectangleType::print();
cout << "; Height = " << height;
Example: }
int *p; //p is a pointer of type int
p = new int; //allocates memory of type int and stores the address
//of the allocated memory in p
*p = 28; //stores 28 in the allocated memory
Operator delete
int *p;
p = new int; //Line 1
*p = 54; //Line 2
p = new int; //Line 3
*p = 73; //Line 4

The previous memory space at


location 1500 is now inaccessible.
This is called memory leak. That is,
there is an unused memory space that
cannot be allocated.
how to avoid memory leak.

delete pointerVariable; //to deallocate a single dynamic variable


delete [] pointerVariable; //to deallocate a dynamic array
Suppose that the definition of the class personType is placed in the header file
Thus, given the
personType.h. declarations
To create the definitionofofthe previous
the class section,the
partTimeEmployee, the
header
statements
file—say, partTimeEmployee.h—must contain the preprocessor directive:
#include "personType.h"
delete p;
delete str;
deallocate the memory spaces to which the pointers p
and str point.
Delete operator

delete p;
delete str;

Depending on a particular system, after these statements


execute, these pointer variables might still contain the addresses
of the deallocated memory spaces.
In this case, we say that these pointers are dangling.

One way to avoid this pitfall is to set these pointers to NULL after
the delete operation.
Operations on Pointer Variables
int *p, *q;
 The statement
 p = q;
 copies the value of q into p. After this statement executes, both p and q
point to the same memory location.
 Any changes made to *p automatically change the value of *q, and vice
versa.
Operations on Pointer Variables
Dynamic Arrays
An array created during the execution of a program is called
a dynamic array.
To create a dynamic array
int *p;
p = new int[10];
allocates 10 contiguous memory locations, each of type int, and stores
the address of the first memory location into p.
*p = 25;
stores 25 into the first memory location, and the statements
p++; //p points to the next array component
*p = 35; store 35 into the second memory location.
 After performing a few increment operations, it is possible to lose
track of the first array component.
 C++ allows us to use array notation to access these memory
locations.
Dynamic Arrays
Dynamic Array

An array name is a constant pointer.


int list[5];
declares list to be an array of five components.
the value of list is constant. Therefore, the increment and
decrement operations
cannot be applied to list.
If p is a pointer variable of type int, then the statement
p = list;
copies the value of list, which is 1000, the base address of the
array, into p.
We are allowed to perform increment and decrement operations
on p.
Functions and Pointers
Dynamic Two-Dimensional Arrays
int **board;
board = new int* [10];
for (int row = 0; row < 10; row++)
board[row] = new int[15];
Classes and Pointers:
How do we ensure that when
p is destroyed ,

if the pointer p does not use the delete


operator to deallocate the dynamic
array, the memory space of the
dynamic array would stay marked as
allocated
Assignment Operator
Lecture 4
finished

You might also like