• No results found

Department of Computer Science and Engineering IIT Bombay

N/A
N/A
Protected

Academic year: 2022

Share "Department of Computer Science and Engineering IIT Bombay"

Copied!
31
0
0

Loading.... (view fulltext now)

Full text

(1)

IIT Bombay

Computer Programming

Dr. Deepak B Phatak Dr. Supratik Chakraborty

Department of Computer Science and Engineering IIT Bombay

Session: Pointers and Dynamic Memory – Part 1

(2)

IIT Bombay

• Variables and pointers to variables in C++

• Storage in stack segment

• “Address of” operator: &

• “Content of” operator: *

Memory locations accessed: local variables/arrays of functions Statically allocated in stack segment when function is called

Quick Recap of Relevant Topics

(3)

IIT Bombay

• Storage in data segment (heap)

• Dynamic allocation of memory in C++

• Accessing dynamically allocated memory

• Good programming practices when using dynamic memory

Overview of This Lecture

(4)

IIT Bombay

Memory For Executing A Program (Process)

• 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 (this lecture)

Stack segment: Call stack MAIN

M EMO R Y

CODE SEGMENT STACK SEGMENT

DATA SEGMENT

(5)

IIT Bombay

Memory For Executing A Program (Process)

Local variables/arrays of a function

• Must be statically declared in function

• Memory allocated in

activation record of function

• Resides in stack segment

• Ceases to exist once function ends and control returns to

caller MAIN M EMO R Y

CODE SEGMENT STACK SEGMENT

DATA SEGMENT

(6)

IIT Bombay

Memory For Executing A Program (Process)

What if the size of a local array is input-dependent?

Examples:

Read number of students in a class and store marks of all

students in an int array.

Read a sequence of

characters ending with “.” and store in a char array

MAIN M EMO R Y

CODE SEGMENT STACK SEGMENT

DATA SEGMENT

(7)

IIT Bombay

Memory For Executing A Program (Process)

What if memory location(s) allocated in a function must be accessed after the function returns?

Examples: A function to read and store a sequence of

characters in a char array, which must be used by the

calling function MAIN

M EMO R Y

CODE SEGMENT STACK SEGMENT

DATA SEGMENT

(8)

IIT Bombay

Memory For Executing A Program (Process)

We need a mechanism for

allocating memory locations dynamically

Dynamic memory allocation:

Allocation at run time

Allocation could depend on values of expressions

read/computed

[number of students,

number of chars in name…+

MAIN M EMO R Y

CODE SEGMENT STACK SEGMENT

DATA SEGMENT

(9)

IIT Bombay

Memory For Executing A Program (Process)

int main() {

int numStudents;

cin >> numStudents;

// Allocate an int array A // of size numStudents // Store quiz marks in A return 0;

} MAIN

M EMO R Y

CODE SEGMENT STACK SEGMENT

DATA SEGMENT

(10)

IIT Bombay

Memory For Executing A Program (Process)

int main() {

int numStudents;

cin >> numStudents;

// Allocate an int array A // of size numStudents // Store quiz marks in A return 0;

} MAIN

M EMO R Y

CODE SEGMENT STACK SEGMENT

DATA SEGMENT

Local variable of function

Memory requirement known at compile-time

(11)

IIT Bombay

Memory For Executing A Program (Process)

int main() {

int numStudents;

cin >> numStudents;

// Allocate an int array A // of size numStudents // Store quiz marks in A return 0;

} MAIN

M EMO R Y

CODE SEGMENT STACK SEGMENT

DATA SEGMENT

Allocate space in activation record of main

(Call stack in stack segment)

(12)

IIT Bombay

Memory For Executing A Program (Process)

int main() {

int numStudents;

cin >> numStudents;

// Allocate an int array A // of size numStudents // Store quiz marks in A return 0;

} MAIN

M EMO R Y

CODE SEGMENT STACK SEGMENT

DATA SEGMENT

Size of array A dependent on value of numStudents.

Memory requirement not known at compile time.

(13)

IIT Bombay

Memory For Executing A Program (Process)

int main() {

int numStudents;

cin >> numStudents;

// Allocate an int array A // of size numStudents // Store quiz marks in A return 0;

} MAIN

M EMO R Y

CODE SEGMENT STACK SEGMENT

DATA SEGMENT

Cannot reserve space for array A when activation

record of “main” is created in stack segment

(14)

IIT Bombay

Memory For Executing A Program (Process)

int main() {

int numStudents;

cin >> numStudents;

// Allocate an int array A // of size numStudents // Store quiz marks in A return 0;

} MAIN

M EMO R Y

CODE SEGMENT STACK SEGMENT

DATA SEGMENT

Where should this memory space come from?

(15)

IIT Bombay

Memory For Executing A Program (Process)

int main() {

int numStudents;

cin >> numStudents;

// Allocate an int array A // of size numStudents // Store quiz marks in A return 0;

} MAIN

M EMO R Y

CODE SEGMENT STACK SEGMENT

DATA SEGMENT

Data Segment

(also called “heap”) to our rescue !

Where should this memory space come from?

(16)

IIT Bombay

Memory For Executing A Program (Process)

int main() {

int numStudents;

cin >> numStudents;

// Allocate an int array A // of size numStudents // Store quiz marks in A return 0;

} MAIN

M EMO R Y

CODE SEGMENT STACK SEGMENT

DATA SEGMENT (HEAP)

How do we allocate an array of size

numStudents at run time in data segment?

(17)

IIT Bombay

Dynamic Memory Allocation in C++

• C++ provides a special construct for dynamically allocating memory in heap (data segment)

A = new int [numStudents];

Dynamically allocate memory on heap

(18)

IIT Bombay

Dynamic Memory Allocation in C++

• C++ provides a special construct for dynamically allocating memory in heap (data segment)

A = new int [numStudents];

Dynamically allocate memory on heap

Allocate space for an array of size “numStudents”

Each element is an int

(19)

IIT Bombay

Dynamic Memory Allocation in C++

• C++ provides a special construct for dynamically allocating memory in heap (data segment)

A = new int [numStudents];

Returns pointer to an int

A should be of type int *

(20)

IIT Bombay

Memory For Executing A Program (Process)

int main() {

int numStudents;

int * A;

cin >> numStudents;

A = new int[numStudents];

// Store quiz marks in A return 0;

} MAIN

M EMO R Y

CODE SEGMENT STACK SEGMENT

DATA SEGMENT (HEAP)

(21)

IIT Bombay

Memory For Executing A Program (Process)

MAIN M EMO R Y

CODE SEGMENT STACK SEGMENT

2 int main()

{

int numStudents;

int * A;

cin >> numStudents;

A = new int[numStudents];

// Store quiz marks in A return 0;

}

DATA SEGMENT (HEAP)

(22)

IIT Bombay

Memory For Executing A Program (Process)

MAIN M EMO R Y

CODE SEGMENT STACK SEGMENT

2 int main()

{

int numStudents;

int * A;

cin >> numStudents;

A = new int[numStudents];

// Store quiz marks in A return 0;

}

DATA SEGMENT (HEAP)

(23)

IIT Bombay

Memory For Executing A Program (Process)

MAIN M EMO R Y

CODE SEGMENT STACK SEGMENT

2

Address 0x024

0x024 int main()

{

int numStudents;

int * A;

cin >> numStudents;

A = new int[numStudents];

// Store quiz marks in A return 0;

}

DATA SEGMENT (HEAP)

(24)

IIT Bombay

Memory For Executing A Program (Process)

MAIN M EMO R Y

CODE SEGMENT STACK SEGMENT

2 0x024 int main()

{

int numStudents;

int * A;

cin >> numStudents;

A = new int[numStudents];

A[0] = 10; A[1] = 15;

return 0;

}

Address

0x024 10

15

DATA SEGMENT (HEAP)

(25)

IIT Bombay

Memory For Executing A Program (Process)

MAIN M EMO R Y

CODE SEGMENT STACK SEGMENT

2 0x024 int main()

{

int numStudents;

int * A;

cin >> numStudents;

A = new int[numStudents];

A[0] = 10; A[1] = 15;

return 0;

}

Address

0x024 10

15

How are addresses of A[0] and A[1] calculated?

DATA SEGMENT (HEAP)

(26)

IIT Bombay

Calculating Addresses for Dynamic Arrays

• Compiler knows

• A is pointer to an integer that is the first in an array of integers

• How? From “A = new int[numStudents];”

• Each element of the array is of int data type

• Each int takes 4 consecutive memory locations (bytes)

• Therefore,

• Address of A[0] is (A + 0)

• Address of A[i] is (A + (4*i))

Address Arithmetic

(27)

IIT Bombay

Generic Format for Dynamic Memory Allocation

• To dynamically allocate memory for a variable of type T T * myVarPtr;

myVarPtr = new T;

Variable accessed as *myVarPtr

• To dynamically allocate memory for an array of n elements of type T

T * myArray;

myArray = new T[n];

Array elements accessed as myArray*0+ … myArray[n-1]

(28)

IIT Bombay

Generic Format for Dynamic Memory Allocation

• To dynamically allocate memory for a variable of type T T * myVarPtr;

myVarPtr = new T;

Variable accessed as *myVarPtr

• To dynamically allocate memory for an array of n elements of type T

T * myArray;

myArray = new T[n];

Array elements accessed as myArray*0+ … myArray[n-1]

Array of type T treated as a variable of type T * that can be

indexed using * … +

“new” returns

pointer to T

in both cases

(29)

IIT Bombay

Good Programming Practices

• Most often, “new” will successfully allocate requested memory from heap and return address to the first allocated byte

However, we cannot take “new” for guaranteed

C++ allows “new” to fail and return 0x0 (also called NULL pointer)

Always check if “new” has returned an address other than 0x0 (NULL) before dereferencing that address

• Avoids unnecessary program crashes especially if program dynamically allocates too much memory

• This is real !!! Programmers encounter this situation in real-life

(30)

IIT Bombay

Dynamic Memory Allocation: The Right Way

int main() {

int numStudents;

int * A;

cin >> numStudents;

A = new int[numStudents];

if (A != NULL) { A[0] = 10; A[1] = 15;}

return 0;

}

Note the check for “new”

having succeeded

(31)

IIT Bombay

Summary

• Dynamically allocating memory on heap (data segment)

• “new” construct in C++

• Accessing dynamically allocated variables and arrays

• Good programming practices when using dynamically

allocated memory

References

Related documents

Computer Science and Engineering Department Indian Institute of Technology

Choice of comparison operator crucially determines sorting order (increasing/decreasing), and also how equal elements

• All local variables of a function allocated space in the activation record of the function..

• Decide which half of array to recurse on based on output of comparison

“padding” (unused memory locations) after locations allocated for different members of a structure..

• Recall how we accessed member data values of structures V3 p, *ptrP;. cin

• Uses dynamically allocated array to store elements Array can grow or shrink in size. • Dynamic memory management

Department of Computer Science and Engineering Indian Institute of Technology Bombay.