Sie sind auf Seite 1von 36

IIT Bombay

Computer Programming
Dr. Deepak B Phatak
Dr. Supratik Chakraborty
Department of Computer Science and Engineering
IIT Bombay
Session: Introduction to Pointers Part 2

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Quick Recap of Relevant Topics


IIT Bombay

Basic programming constructs


Pointer data type in C++
Address of operator in C++
Caveats when using address of operator

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Overview of This Lecture


IIT Bombay

Dereferencing a memory address


Finding content at a given memory address

Contents of operator in C++

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Memory and Addresses


IIT Bombay

Address (in hexadecimal)


Memory is a sequence of
400
10011101
401
physical storage locations
10111111
402
10010001
Each location stores 1 byte (8
403
10110111
bits): Content/value of location
404
10010001
405
10000111
Each physical memory location
406
11110001
identified by a unique address
407
10000000
Index in sequence of memory
408
11111111
locations
00000000
409
11110000
40a

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Memory For Executing A Program (Process)

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

STACK SEGMENT

DATA

SEGMENT
CODE SEGMENT

MAIN

Operating system allocates a


part of main memory for use
by a process
Divided into:
Code segment: Stores
executable instructions in
program
Data segment: For
dynamically allocated data
(later lecture)
Stack segment: Call stack

MEMORY

IIT Bombay

Accessing Content At Given Address


Memory Address
int main()
{
int a;
int * ptrA;
0x00402 0x00405
a = 0x02abc;
ptrA = &a;
0x0040a -

STACK SEGMENT

MEMORY

IIT Bombay

0x02abc

// Rest of code
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

DATA SEGMENT
CODE SEGMENT

MAIN

0x0040d

Accessing Content At Given Address

0x0040d

// Rest of code
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

STACK SEGMENT

0x02abc
0x00402
DATA SEGMENT
CODE SEGMENT

MAIN

Memory Address
int main()
{
int a;
int * ptrA;
0x00402 0x00405
a = 0x02abc;
ptrA = &a;
0x0040a -

MEMORY

IIT Bombay

Accessing Content At Given Address


Memory Address
int main()
{
Can we access value of a knowing value of ptrA ?
int a;
STACK SEGMENT
int * ptrA;
0x00402 0x02abc
0x00405
a = 0x02abc;
ptrA = &a;
0x0040a 0x00402

MEMORY

IIT Bombay

// Rest of code
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

DATA SEGMENT
CODE SEGMENT

MAIN

0x0040d

Accessing Content At Given Address


IIT Bombay

C++ provides a content of operator: unary *


If ptrA is a program variable of type int *,
* ptrA gives the integer stored at address given by
ptrA

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Accessing Content At Given Address

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

STACK SEGMENT

0x02abc
0x00402
DATA SEGMENT
CODE SEGMENT

MAIN

Memory Address
int main()
{
int a;
int * ptrA;
0x00402 0x00405
a = 0x02abc;
ptrA = &a;
0x0040a cout << *ptrA; 0x0040d
// Rest of code
}

MEMORY

IIT Bombay

10

Accessing Content At Given Address

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

MAIN

Memory Address
int main()
Find content/value at address given by
{
value of ptrA
int a;
STACK SEGMENT
int * ptrA;
0x00402 0x02abc
0x00405
a = 0x02abc;
ptrA = &a;
0x0040a 0x00402
cout << *ptrA; 0x0040d
DATA SEGMENT
// Rest of code
CODE SEGMENT
}

MEMORY

IIT Bombay

11

Accessing Content At Given Address

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

MAIN

Memory Address
int main()
{ Find content/value at address 0x00402
int a;
STACK SEGMENT
int * ptrA;
0x00402 0x02abc
0x00405
a = 0x02abc;
ptrA = &a;
0x0040a 0x00402
cout << *ptrA; 0x0040d
DATA SEGMENT
// Rest of code
CODE SEGMENT
}

MEMORY

IIT Bombay

12

Accessing Content At Given Address

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

MAIN

Memory Address
int main()
{ How many bytes to read starting from
address 0x00402?
int a;
STACK SEGMENT
int * ptrA;
0x00402 0x02abc
0x00405
a = 0x02abc;
ptrA = &a;
0x0040a 0x00402
cout << *ptrA; 0x0040d
DATA SEGMENT
// Rest of code
CODE SEGMENT
}

MEMORY

IIT Bombay

13

Accessing Content At Given Address

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

MAIN

Address
int Since
main()ptrA isMemory
a pointer
to int , its value is
address of an integer (4 bytes),
{
intso
a; read 4 bytes starting from 0x00402
STACK SEGMENT
int * ptrA;
0x00402 0x02abc
0x00405
a = 0x02abc;
ptrA = &a;
0x0040a 0x00402
cout << *ptrA; 0x0040d
DATA SEGMENT
// Rest of code
CODE SEGMENT
}

MEMORY

IIT Bombay

14

Accessing Content At Given Address

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

STACK SEGMENT

0x02abc
0x00402
DATA SEGMENT
CODE SEGMENT

MAIN

Memory Address
int main()
Read 0x02abc and print it
{
int a;
int * ptrA;
0x00402 0x00405
a = 0x02abc;
ptrA = &a;
0x0040a cout << *ptrA; 0x0040d
// Rest of code
}

MEMORY

IIT Bombay

15

Accessing Content At Given Address


IIT Bombay

C++ provides a content of operator: unary *


*ptrA gives the content at address given by ptrA
Unary operator: Takes a single argument
* ptrA is a C++ expression
Worry about operator precedence, associativity???
Simplify life: use parentheses
Dereferencing an address: Accessing content at that address
Can have spaces after & and *: use carefully
Which is more understandable? &a or & a, *ptrA or * ptrA
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

16

Can We Have Dereferences of Dereferences?


IIT Bombay

int main()
Memory Address
{ char c;
0x00302
char * ptrC;
0x00402 char ** ptrPtrC;
0x00405
ptrC = &c;
ptrPtrC = &ptrC;
0x0040a cin >> c;
0x0040d
cout << *(*ptrPtrC);
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

STACK SEGMENT

17

Can We Have Dereferences of Dereferences?


IIT Bombay

int main()
Memory Address
{ char c;
0x00302
char * ptrC;
0x00402 char ** ptrPtrC;
0x00405
ptrC = &c;
ptrPtrC = &ptrC;
0x0040a cin >> c;
0x0040d
cout << *(*ptrPtrC);
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

STACK SEGMENT

0x00302

18

Can We Have Dereferences of Dereferences?


IIT Bombay

int main()
Memory Address
{ char c;
0x00302
char * ptrC;
0x00402 char ** ptrPtrC;
0x00405
ptrC = &c;
ptrPtrC = &ptrC;
0x0040a cin >> c;
0x0040d
cout << *(*ptrPtrC);
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

STACK SEGMENT

0x00302
0x00402

19

Can We Have Dereferences of Dereferences?


IIT Bombay

int main()
Memory Address
{ char c;
0x00302
char * ptrC;
0x00402 char ** ptrPtrC;
0x00405
ptrC = &c;
ptrPtrC = &ptrC;
0x0040a cin >> c;
0x0040d
cout << *(*ptrPtrC);
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

STACK SEGMENT

0x00030
0x00302
0x00402

20

Can We Have Dereferences of Dereferences?


IIT Bombay

int main()
Memory Address
{ char c;
0x00302
char * ptrC;
0x00402 char ** ptrPtrC;
0x00405
ptrC = &c;
ptrPtrC = &ptrC;
0x0040a cin >> c;
0x0040d
cout << *(*ptrPtrC);
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

STACK SEGMENT

0x00030
0x00302
0x00402

21

Can We Have Dereferences of Dereferences?


IIT Bombay

int main()
Memory Address
STACK SEGMENT
{ char c;
0x00302
0x00030
char * ptrC;
0x00402 char ** ptrPtrC;
0x00302
0x00405
ptrC = &c;
ptrPtrC = &ptrC;
0x0040a 0x00402
cin >> c;
0x0040d
cout << *(*ptrPtrC);
return 0;
What is the value of *ptrPtrC ?
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

22

Can We Have Dereferences of Dereferences?


IIT Bombay

int main()
Memory Address
STACK SEGMENT
{ char c;
0x00302
0x00030
char * ptrC;
0x00402 char ** ptrPtrC;
0x00302
0x00405
ptrC = &c;
ptrPtrC = &ptrC;
0x0040a 0x00402
cin >> c;
0x0040d
cout << *(*ptrPtrC);
return 0;
ptrPtrC has value 0x00402
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

23

Can We Have Dereferences of Dereferences?


IIT Bombay

int main()
Memory Address
STACK SEGMENT
{ char c;
0x00302
0x00030
char * ptrC;
0x00402 char ** ptrPtrC;
0x00302
0x00405
ptrC = &c;
ptrPtrC = &ptrC;
0x0040a 0x00402
cin >> c;
0x0040d
cout << *(*ptrPtrC);
*ptrPtrC has value
return 0;
content of memory at address 0x00402
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

24

Can We Have Dereferences of Dereferences?


IIT Bombay

int main()
Memory Address
STACK SEGMENT
{ char c;
0x00302
0x00030
char * ptrC;
0x00402 char ** ptrPtrC;
0x00302
0x00405
ptrC = &c;
ptrPtrC = &ptrC;
0x0040a 0x00402
cin >> c;
0x0040d
cout << *(*ptrPtrC);
Content at address 0x00402:
return 0;
How many bytes to read?
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

25

Can We Have Dereferences of Dereferences?


IIT Bombay

int main()
Memory Address
STACK SEGMENT
{ char c;
0x00302
0x00030
char * ptrC;
0x00402 char ** ptrPtrC;
0x00302
0x00405
ptrC = &c;
ptrPtrC = &ptrC;
0x0040a 0x00402
cin >> c;
0x0040d
cout << *(*ptrPtrC);
How many bytes to read?
return 0;
ptrPtrC is pointer to what data type?
}
pointer (4 bytes)
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

26

Can We Have Dereferences of Dereferences?


IIT Bombay

int main()
Memory Address
STACK SEGMENT
{ char c;
0x00302
0x00030
char * ptrC;
0x00402 char ** ptrPtrC;
0x00302
0x00405
ptrC = &c;
ptrPtrC = &ptrC;
0x0040a 0x00402
cin >> c;
0x0040d
cout << *(*ptrPtrC);
To get *ptrPtrC, read 4 bytes
return 0;
starting from address 0x00402
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

27

Can We Have Dereferences of Dereferences?


IIT Bombay

int main()
Memory Address
STACK SEGMENT
{ char c;
0x00302
0x00030
char * ptrC;
0x00402 char ** ptrPtrC;
0x00302
0x00405
ptrC = &c;
ptrPtrC = &ptrC;
0x0040a 0x00402
cin >> c;
0x0040d
cout << *(*ptrPtrC);
*ptrPtrC has value 0x00302
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

28

Can We Have Dereferences of Dereferences?


IIT Bombay

int main()
Memory Address
STACK SEGMENT
{ char c;
0x00302
0x00030
char * ptrC;
0x00402 char ** ptrPtrC;
0x00302
0x00405
ptrC = &c;
ptrPtrC = &ptrC;
0x0040a 0x00402
cin >> c;
0x0040d
cout << *(*ptrPtrC);
What is * (*ptrPtrC) ?
return 0;
content of memory at address 0x00302
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

29

Can We Have Dereferences of Dereferences?


IIT Bombay

int main()
Memory Address
STACK SEGMENT
{ char c;
0x00302
0x00030
char * ptrC;
0x00402 char ** ptrPtrC;
0x00302
0x00405
ptrC = &c;
ptrPtrC = &ptrC;
0x0040a 0x00402
cin >> c;
0x0040d
cout << *(*ptrPtrC);
Content at address 0x00302:
return 0;
How many bytes to read?
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

30

Can We Have Dereferences of Dereferences?


IIT Bombay

int main()
Memory Address
STACK SEGMENT
{ char c;
0x00302
0x00030
char * ptrC;
0x00402 char ** ptrPtrC;
0x00302
0x00405
ptrC = &c;
ptrPtrC = &ptrC;
0x0040a 0x00402
cin >> c;
0x0040d
cout << *(*ptrPtrC);
How many bytes to read?
return 0;
*ptrPtrC is pointer to what data type?
}
char (1 byte)
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

31

Can We Have Dereferences of Dereferences?


IIT Bombay

int main()
Memory Address
STACK SEGMENT
{ char c;
0x00302
0x00030
char * ptrC;
0x00402 char ** ptrPtrC;
0x00302
0x00405
ptrC = &c;
ptrPtrC = &ptrC;
0x0040a 0x00402
cin >> c;
0x0040d
cout << *(*ptrPtrC);
To get *(*ptrPtrC), read 1 byte
return 0;
starting from address 0x00302
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

32

Can We Have Dereferences of Dereferences?


IIT Bombay

int main()
Memory Address
STACK SEGMENT
{ char c;
0x00302
0x00030
char * ptrC;
0x00402 char ** ptrPtrC;
0x00302
0x00405
ptrC = &c;
ptrPtrC = &ptrC;
0x0040a 0x00402
cin >> c;
0x0040d
cout << *(*ptrPtrC);
*(*ptrPtrC) has value 0x00030
return 0;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

33

How Far Can We Nest Dereferences?


IIT Bombay

No pre-specified limit
If x is a variable of type int ****, we can use upto four
levels of dereferencing of x

Declaration: int **** x;


*x: expression of type int *** [Think int *** *x;]
*(*x): expression of type int ** [Think int ** **x;]
*(*(*x)): expression of type int * [Think int * ***x;]
*(*(*(*x))): expression of type int [Think int ****x;]
*(*(*(*(*x)))): Compilation error, since *(*(*(*x))) is not of
pointer type, and cannot be dereferences

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

34

Caveats When Dereferencing


IIT Bombay

Certain memory addresses outside the part of memory


allocated to process by operating system
Dereferencing such an address leads to runtime error
Segmentation violation
Program aborts/crashes

Memory location with address 0x0 is never within any user


process memory space
Dereferencing 0x0 will certainly cause program to crash

Need to be careful that we are dereferencing addresses of


memory locations allocated to process
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

35

Summary
IIT Bombay

Dereferencing a memory address


Finding content at given address

Content of operator in C++


Caveats when using content of operator
Deferencing bad addresses

We can now access memory locations through their addresses

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

36

Das könnte Ihnen auch gefallen