Procedural Concept: - The Main Program Coordinates Calls To Procedures and Hands Over Appropriate Data As Parameters
Procedural Concept: - The Main Program Coordinates Calls To Procedures and Hands Over Appropriate Data As Parameters
3
Pointers, Dynamic Data, and
Reference Types
• Review on Pointers
• Reference Variables
• Dynamic Memory Allocation
– The new operator
– The delete operator
– Dynamic Memory Allocation for Arrays
4
C++ Data Types
simple structured
pointer reference
5
char str [ 8 ];
6000
6
Addresses in Memory
• When a variable is declared, enough memory to hold a
value of that type is allocated for it at an unused memory
location. This is the address of the variable
int x;
float number;
char ch;
x number ch
7
Obtaining Memory Addresses
• The address of a non-array variable can be obtained by using
the address-of operator &
8
What is a pointer variable?
9
Using a Pointer Variable
2000
int x;
12
x = 12;
x
3000
int* ptr;
2000
ptr = &x;
ptr
3000
int* ptr;
2000
ptr = &x; ptr
3000
int* ptr;
2000
ptr = &x; ptr
*ptr = 5;
// changes the value at the
address ptr points to 5
12
Self –Test on Pointers
4000
char ch;
A Z
ch = ‘A’;
ch
ptr
14
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;
16
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;
17
}
Dynamic Memory Allocation
In C and C++, three types of memory are used by programs:
18
3 Kinds of Program Data
• STATIC DATA: Allocated at compiler time
R u n -tim e a llo c a te d
S ta c k
m e m o ry
H eap
C o m p ile -tim e
s ta tic d a ta
a llo c a te d
m e m o ry
P ro g ra m
code
L o w -e n d
20
Dynamic Memory Allocation
• In C, functions such as malloc() are used to
dynamically allocate memory from the Heap.
• In C++, this is accomplished using the new and
delete operators
• new is used to allocate memory during execution time
– returns a pointer to the address where the object is
to be stored
– always returns a pointer to the type that follows the
new
21
Operator new Syntax
new DataType
ptr
ptr = new char;
5000
*ptr = ‘B’;
‘B’
cout << *ptr;
23
The NULL Pointer
• There is a pointer constant called the “null pointer”
denoted by NULL
• But NULL is not memory address 0.
delete Pointer
delete [ ] Pointer
26
Example
char *ptr ;
3000
ptr
ptr = new char[ 5 ]; 6000
NULL
???
???
27
Pointers and Constants
char* p;
p = new char[20];