Sei sulla pagina 1di 27

IIT Bombay

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

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

Quick Recap of Relevant Topics


IIT Bombay

Basic programming constructs


Variables and basic data types
int, float, double, char, bool, void

Arrays and matrices


Programs to solve some interesting problems
Variables: Named memory locations
Memory locations accessed through names
2

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

Overview of This Lecture


IIT Bombay

Addresses of memory locations


Address of operator in C++
Pointer data type in C++
Motivate accessing memory locations through addresses

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

Memory and Addresses

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

MAIN

Address (in hexadecimal)


Main 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

MEMORY

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

Program Variables and Memory


IIT Bombay

int main()
{
int a;
float b;
char c;

Named drawers of Dumbo

Named locations in stack segment

// Rest of code
}

What are their addresses in memory?

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

Memory and Program Variables


int main()
{
4 bytes for a in stack segment STACK SEGMENT
int a; Each addressable memory location stores 1 byte
float b;
char c;

MEMORY

IIT Bombay

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

CODE SEGMENT

MAIN

DATA SEGMENT

Memory and Program Variables


int main()
{
int a;
float b;
char c;

STACK SEGMENT

MEMORY

IIT Bombay

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

CODE SEGMENT

MAIN

DATA SEGMENT

Memory and Program Variables


int main()
{
int a;
float b;
char c;

4 bytes for b in stack segment


STACK SEGMENT

MEMORY

IIT Bombay

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

CODE SEGMENT

MAIN

DATA SEGMENT

Memory and Program Variables


int main()
{
int a;
float b;
char c;

1 byte for c in stack segment


STACK SEGMENT

MEMORY

IIT Bombay

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

CODE SEGMENT

MAIN

DATA SEGMENT

10

Memory and Program Variables


int main()
{
int a;
float b;
char c;

Memory Address

0x00104
0x00200 0x00203

STACK SEGMENT

MEMORY

IIT Bombay

0x00402 0x00405

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

CODE SEGMENT

MAIN

DATA SEGMENT

11

Memory and Program Variables


0x00104
0x00200 0x00203

STACK SEGMENT

0x00402 0x00405
DATA SEGMENT

Accessing memory
locations by names

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

CODE SEGMENT

MAIN

int main()
{
int a;
float b;
char c;
cin >> a;
cin >> c;
b = (a + c)/2.0;
return 0;
}

Memory Address

MEMORY

IIT Bombay

12

Memory and Program Variables

???

0x00104
0x00200 0x00203

STACK SEGMENT

0x00402 0x00405
DATA SEGMENT

Can we access
memory locations by
their addresses?

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

CODE SEGMENT

MAIN

int main()
{
int a;
float b;
char c;
cin >> a;
cin >> b;
c = (a + b)/2;
}

Memory Address

MEMORY

IIT Bombay

13

Memory and Program Variables

???

0x00104
0x00200 0x00203

STACK SEGMENT

0x00402 0x00405
DATA SEGMENT

YES (this lecture and next) !!!


CODE SEGMENT
If we can find the addresses
of memory
locations corresponding to variables

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

MAIN

int main()
{
int a;
float b;
char c;
cin >> a;
cin >> b;
c = (a + b)/2;
}

Memory Address

MEMORY

IIT Bombay

14

How Do We Get an Address?


IIT Bombay

C++ provides an address of operator: unary &

If a is a program variable, &a gives address of a


in memory
Unary operator: Takes a single argument
&a is a C++ expression
Worry about operator precedence, associativity???
Simplify life: use parentheses

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

15

Pointer as a Type
IIT Bombay

If a is an int variable, what is the type of &a


Pointer to int, written in C++ as the type int *
If b is a float variable, type of &b is pointer to float,
written as float *
In general, if x is a variable of type T
T * is the type pointer to T
&x is an expression of type T *, gives address of x
If int * is a type, cant we have a variable of type int *?
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

16

Pointer Variables in a C++ Program


int main()
0x00104
{
STACK SEGMENT
0x00200 int a;
0x00203
float b;
0x00402 char c;
0x00405
int * ptrA;
0x0040a DATA SEGMENT
// Rest of code 0x0040d
SEGMENT
} Assume, all memory addresses areCODE
32 bits (4 bytes)
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

MAIN

Memory Address

MEMORY

IIT Bombay

17

Pointer Variables in a C++ Program

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

STACK SEGMENT

DATA SEGMENT
CODE SEGMENT

MAIN

Memory Address
int main()
0x00104
{
0x00200 int a;
0x00203
float b;
0x00402 char c;
0x00405
int * ptrA;
0x0040a ptrA = &a;
0x0040d
// Rest of code
}

MEMORY

IIT Bombay

18

Pointer Variables in a C++ Program

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

MAIN

Memory Address
int main()
0x00104
{
STACK SEGMENT
0x00200
int a;
0x00203
float b;
0x00402 char c;
0x00405
int * ptrA;
0x0040a ptrA = &a;
0x0040d
DATA SEGMENT
// Rest of code
What is the value assigned CODE
to ptrA
?
SEGMENT
}
0x00402, 0x00403, 0x00404 or 0x00405 ?

MEMORY

IIT Bombay

19

Pointer Variables in a C++ Program

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

MAIN

Memory Address
int main()
0x00104
{
STACK SEGMENT
0x00200
int a;
0x00203
float b;
0x00402 char c;
0x00405
int * ptrA;
0x00402
0x0040a ptrA = &a;
0x0040d
DATA SEGMENT
// Rest of code
What is the value assigned CODE
to ptrA
?
SEGMENT
} 0x00402: Address of first among bytes allocated for a

MEMORY

IIT Bombay

20

Value and Address of Pointer Variables

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

MAIN

Memory Address
int main()
0x00104
{
STACK SEGMENT
0x00200
int a;
0x00203
float b;
0x00402 char c;
0x00405
int * ptrA;
0x00402
0x0040a ptrA = &a;
0x0040d
DATA SEGMENT
// Rest of code
Content/Value of ptrA: 0x00402
CODE SEGMENT
}
Address of ptrA: 0x0040a

MEMORY

IIT Bombay

21

Can We Have Pointers to Pointers?


IIT Bombay

ptrA is a variable of type int * (pointer to int)


What is the type of the expression &ptrA?
Recall: If variable x is of type T, &x is of type T *
Type of &ptrA is int **: Pointer to pointer to int
Note: We dont write (int *) *, but int **
How far can we take this?
As far as we want
int **** is a legitimate pointer type in C++
pointer to pointer to pointer to pointer to int
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

22

A Note About Pointer Declarations


IIT Bombay

int main()
{
Types of x, y and z: int
int x, y, z;
int *a, b, *c;
// Rest of code
}

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

23

A Note About Pointer Declarations


IIT Bombay

int main()
{
Type of a: pointer to int
int x, y, z;
Type of c: pointer to int,
int *a, b, *c;
not pointer to pointer to int
// Rest of code
}
Type of b: int,
not pointer to int

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

24

A C++ Program For Printing Addresses


IIT Bombay

int main()
{
int a; float b; char c;
int * ptrA; float * ptrB; char * ptrC;
ptrA = &a; cout << Address of a is: << ptrA;
ptrB = &b; cout << Address of b is: << ptrB;
ptrC = &c; cout << Address of c is: << ptrC;
return 0;
Compile and run this program
}

See how memory addresses look like !!!

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

25

A C++ Program For Printing Addresses


IIT Bombay

int main()
{
int a; float b; char c;
int * ptrA; float * ptrB; char * ptrC;
ptrA = &a; cout << Address of a is: << ptrA;
ptrB = &b; cout << Address of b is: << ptrB;
ptrC = &c; cout << Address of c is: << ptrC;
return 0;
What do we do with these addresses?
}

Wait for a few more lectures !!!

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

26

Summary
IIT Bombay

Memory and addresses


Address of operator in C++
Pointer data type in C++
Some simple usage in programs

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

27

Potrebbero piacerti anche