01 Lecture One
01 Lecture One
Review On Pointers
2
Memory Allocation
0 1 2 3 4
int x;
x=24;
x
24
cout<< X; // 24 Print value
cout<< &X; //
of 6
x Print address of x
Float y;
y=81.36; 81.3 6
y
cout<< y; // 81.36 Print value of x
cout<< &y; // 26 Print address of x Memory 5
Address Operator &
• Each variable in a program is stored at a unique
location in memory that has an address.
• Use the address operator & to get the address of a
variable:
int num = -23;
cout << # // prints address in
hexadecimal
5
Pointer
s
• Definition:
int *p;
• Read as:
“p can hold the address of an int” or
“the variable that p points to has type int”
• The spacing in the definition does not matter:
int * p;
int*
p;
6
• * is called
Pointer Variables
• Definition and assignment:
int num = 25;
int *p;
p = #
• Memory num p
layout:
25 0x4a0
address of num: 0x4a00 0
• You can access num using p and indirection operator *:
cout << p; // prints 0x4a00
cout << *p; // prints 25
9
*p = 20; // puts 20 in num
Pointer Variables
p = &i;
q =
&i; r
= p;
8
Pointer Initialization
• You can initialize to NULL or 0 (zero)
int *ptr = 0;
• You can initialize to addresses of other variables
int num, *numPtr = #
int val[10], *valptr = val;
• The initial value must have the correct type
float cost;
int *ptr = &cost; // won't work
9
Pointer Variables
firstvalue is 10
#include <iostream> secondvalue is 20
using namespace std;
int main ()
{
int firstvalue, secondvalue;
int * mypointer;
mypointer = &firstvalue;
*mypointer = 10;
mypointer = &secondvalue;
*mypointer = 20;
cout << "firstvalue is "
<< firstvalue << endl;
cout << "secondvalue is "
<< secondvalue << endl;
return 0;
}
10
Pointer Variables
firstvalue is 10
#include <iostream> secondvalue is 20
using namespace std;
int main ()
{
int firstvalue = 5, secondvalue = 15;
int * p1, * p2;
p1 = &firstvalue; // p1 = address of
firstvalue p2 = &secondvalue; // p2 = address of
secondvalue
*p1 = 10; // value pointed by p1 = 10
*p2 = *p1; // value pointed by p2 = value pointed by p1
p1 = p2; // p1 = p2 (value of pointer is copied)
*p1 = 20; // value pointed by p1 = 20
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
return 0;
}
11
Common Bugs
• Bug #1 - Uninitialized pointers
• It occurs when you try to reference the value of
a pointer even though the pointer is
uninitialized and does not yet point to a valid
address
int *p;
*p = 12; //Uninitialized pointer
12
Common Bugs
• Bug #2 - Invalid Pointer References
• An invalid pointer reference occurs when a
pointer's value is referenced even though the
pointer doesn't point to a valid block.
int *q,*p;
p = q;
*p=25; //invalid pointer reference
13
Common Bugs
• Bug #3 - Zero Pointer Reference
• A zero pointer reference occurs whenever a pointer
pointing to zero is used in a statement that attempts
to reference a block.
15
Pointers and arrays
• What is valptr + 1? 4 7 11
s
t
a
r
t
i
n
g
17
Pointers and arrays
10, 20, 30, 40, 50,
#include <iostream>
using namespace std;
int main ()
{
int numbers[5];
int * p;
p = numbers;
*p = 10; p++;
*p = 20;
p =
&numbers[2];
*p = 30;
p = numbers +
3;
*p = 40;
p = numbers;
*(p+4) = 50;
for (int n=0;
n<5; n++)
cout << numbers[n] << ", "; 18
return 0;
Pointer Arithmetics
23
Pointer Arithmetics
• Suppose that we define three pointers:
char *mychar;
short *myshort;
long *mylong;
• Assume we know
that they point to
memory locations
1000, 2000 and
3000 respectively
• If we write:
mychar++; 24
Pointer Arithmetics
mychar = mychar + 1;
myshort = myshort + 1;
mylong = mylong + 1;
25
Pointer Arithmetics
• The following expression may lead to confusion:
*p++;
(*p)++
26
27