• No results found

• Storage on stack segment and data segment (heap)

N/A
N/A
Protected

Academic year: 2022

Share "• Storage on stack segment and data segment (heap)"

Copied!
28
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 2

(2)

IIT Bombay

• Variables and pointers to variables in C++

• “Address of” operator: &

• “Content of” operator: *

• Storage on stack segment and data segment (heap)

• “new” construct in C++

Quick Recap of Relevant Topics

(3)

IIT Bombay

• Persistence of dynamically allocated memory

• Explicitly de-allocating dynamically allocated memory

• Good programming practices when de-allocating dynamic memory

Overview of This Lecture

(4)

IIT Bombay

Dynamically Allocating Memory in a Function

int * readQuizMarks(int n);

int main()

{ int numStudents; int * qMarks;

cout << “Given student count: “ << endl;

cin >> numStudents;

cout << “Give marks of students” << endl;

qMarks = readQuizMarks(numStudents);

// Print max and min marks

(5)

IIT Bombay

Dynamically Allocating Memory in a Function

int * readQuizMarks(int n);

int main()

{ int numStudents; int * qMarks;

cout << “Given student count: “ << endl;

cin >> numStudents;

cout << “Give marks of students” << endl;

qMarks = readQuizMarks(numStudents);

// Print max and min marks return 0;

// PRECONDITION: n > 0 int * readQuizMarks(int n) { int * marks, i;

marks = new int[n];

if (marks == NULL) {return NULL;}

for (i = 0; i<n; i++) {cin >> marks[i];}

return marks;

}

// POSTCONDITION: Marks stored

(6)

IIT Bombay

Dynamically Allocating Memory in a Function

Stack SegmentData Segment (Heap)

AR of

main 2 numStudents qMarks

int main()

{ int numStudents; int * qMarks;

cout << “Given student count: “ << endl;

cin >> numStudents;

cout << “Give marks of students” << endl;

qMarks = readQuizMarks(numStudents);

// Print max and min marks

(7)

IIT Bombay

Dynamically Allocating Memory in a Function

int * readQuizMarks(int n) { int * marks, i;

marks = new int[n];

if (marks == NULL) { return NULL;

}

for (i = 0; i < n; i++) { cin >> marks[i];

}

return marks;

Stack SegmentData Segment (Heap)

AR of main AR of readQuizMarks

2 numStudents qMarks

(8)

IIT Bombay

Dynamically Allocating Memory in a Function

Stack SegmentData Segment (Heap) 2 n

AR of main AR of

readQuizMarks marks

2 numStudents qMarks

int * readQuizMarks(int n) { int * marks, i;

marks = new int[n];

if (marks == NULL) { return NULL;

}

for (i = 0; i < n; i++) { cin >> marks[i];

}

return marks;

(9)

IIT Bombay

Dynamically Allocating Memory in a Function

Stack SegmentData Segment (Heap) 2 n

AR of main AR of

readQuizMarks 0x400 marks

Address 0x400 0x404

2 numStudents qMarks

int * readQuizMarks(int n) { int * marks, i;

marks = new int[n];

if (marks == NULL) { return NULL;

}

for (i = 0; i < n; i++) { cin >> marks[i];

}

return marks; marks[1]marks[0]

(10)

IIT Bombay

Dynamically Allocating Memory in a Function

Stack SegmentData Segment (Heap) 2 n

AR of main AR of

readQuizMarks 0x400 marks

Address 0x400

0x404 10 marks[0]

15 marks[1]

2 numStudents qMarks

int * readQuizMarks(int n) { int * marks, i;

marks = new int[n];

if (marks == NULL) { return NULL;

}

for (i = 0; i < n; i++) { cin >> marks[i];

}

return marks;

(11)

IIT Bombay

Dynamically Allocating Memory in a Function

Stack SegmentData Segment (Heap) 2 n

AR of main AR of

readQuizMarks 0x400 marks

Address 0x400

0x404 10 marks[0]

15 marks[1]

2 numStudents qMarks

int * readQuizMarks(int n) { int * marks, i;

marks = new int[n];

if (marks == NULL) { return NULL;

}

for (i = 0; i < n; i++) { cin >> marks[i];

}

return marks;

(12)

IIT Bombay

Can Caller Access Memory Allocated by Callee?

Stack SegmentData Segment (Heap)

AR of main

Address 0x400

0x404 10 15

int main()

{ int numStudents; int *qMarks;

cout << “Given student count: “ << endl;

cin >> numStudents;

cout << “Give marks of students” << endl;

qMarks = readQuizMarks(numStudents);

// Print max and min marks

2 numStudents 0x400 qMarks

(13)

IIT Bombay

Can Caller Access Memory Allocated by Callee?

Stack SegmentData Segment (Heap)

AR of main

Address 0x400 0x404

int main()

{ int numStudents; int *qMarks;

cout << “Given student count: “ << endl;

cin >> numStudents;

cout << “Give marks of students” << endl;

qMarks = readQuizMarks(numStudents);

// Print max and min marks

2 numStudents 0x400 qMarks

Activation record of

readQuizMarks non-existent in stack segment

1015

(14)

IIT Bombay

Can Caller Access Memory Allocated by Callee?

Stack SegmentData Segment (Heap)

AR of main

Address 0x400 0x404

int main()

{ int numStudents; int *qMarks;

cout << “Given student count: “ << endl;

cin >> numStudents;

cout << “Give marks of students” << endl;

qMarks = readQuizMarks(numStudents);

// Print max and min marks

2 numStudents 0x400 qMarks

Activation record of

readQuizMarks non-existent in stack segment

Memory dynamically allocated

1015

(15)

IIT Bombay

Can Caller Access Memory Allocated by Callee?

Stack SegmentData Segment (Heap)

AR of main

Address 0x400 0x404

int main()

{ int numStudents; int *qMarks;

cout << “Given student count: “ << endl;

cin >> numStudents;

cout << “Give marks of students” << endl;

qMarks = readQuizMarks(numStudents);

// Print max and min marks

2 numStudents 0x400 qMarks

“main” has address of memory dynamically allocated in

readQuizMarks.

Can access this memory from “main”

1015

(16)

IIT Bombay

Accessing Memory Allocated by Callee

Stack SegmentData Segment (Heap)

AR of main

Address 0x400 0x404

int main() { … … …

if (qMarks == NULL) { return -1; }

int i, min = qMarks[0], max = qMarks[0];

for (i = 1; i < numStudents; i++) {

if (qMarks[i] < min) { min = qMarks[i];}

if (qMarks[i] > max) {max = qMarks[i];}

}

cout << “Min: “ << min << endl;

2 numStudents 0x400 qMarks

1015

(17)

IIT Bombay

Accessing Memory Allocated by Callee

Stack SegmentData Segment (Heap)

AR of main

Address 0x400 0x404

2 numStudents 0x400 qMarks

int main() { … … …

if (qMarks == NULL) { return -1; }

int i, min = qMarks[0], max = qMarks[0];

for (i = 1; i < numStudents; i++) {

if (qMarks[i] < min) { min = qMarks[i];}

if (qMarks[i] > max) {max = qMarks[i];}

}

cout << “Min: “ << min << endl;

cout << “ Max: “ << max << endl;

When is this memory

freed (de-allocated) ?

10

15

(18)

IIT Bombay

Freeing Dynamically Allocated Memory

• Memory dynamically allocated by a function not

automatically de-allocated (freed) when the function returns

• Unless explicitly de-allocated, dynamically allocated memory persists until program ends execution

• Can be very wasteful of memory

(19)

IIT Bombay

Persistence of Dynamically Allocated Memory

int * altReadQuizMarks(int n) { int * marks, * temp, i;

marks = new int[n];

temp = new int[n];

if ((marks == NULL) || (temp == NULL)) { return NULL; }

for (i = 0; i < n; i++) { cin >> marks[i];

temp[i] = marks[i] + 10;

}

// Some computation with temp return marks;

Stack SegmentData Segment (Heap) 2 n

AR of main AR of

altReadQuizMarks 0x400 marks

Address 0x400

0x404 10 marks[0]

15 marks[1]

2 numStudents qMarks

0x600 temp

(20)

IIT Bombay

Persistence of Dynamically Allocated Memory

Stack SegmentData Segment (Heap)

AR of main

Address 0x400

0x404 10 15

2 numStudents 0x400 qMarks

int main()

{ int numStudents; int *qMarks;

cout << “Given student count: “ << endl;

cin >> numStudents;

cout << “Give marks of students” << endl;

qMarks = altReadQuizMarks(numStudents);

// Print max and min marks from qMarks

(21)

IIT Bombay

Persistence of Dynamically Allocated Memory

Stack SegmentData Segment (Heap)

AR of

main 2 numStudents 0x400 qMarks

int main()

{ int numStudents; int *qMarks;

cout << “Given student count: “ << endl;

cin >> numStudents;

cout << “Give marks of students” << endl;

qMarks = altReadQuizMarks(numStudents);

// Print max and min marks from qMarks

Address 0x400

0x404 10 15

Array temp of no use, but persists in heap.

Wasted memory space.

(22)

IIT Bombay

Explicitly De-allocating Dynamic Memory

• C++ provides a special construct to explicitly de-allocate dynamically allocated memory

Allocation: T * myVar; De-allocation: delete myVar;

myVar = new T;

Allocation: T * myArray; De-allocation: delete[] myArray;

(23)

IIT Bombay

Explicitly De-allocating Dynamic Memory

• C++ provides a special construct to explicitly de-allocate dynamically allocated memory

Allocation: T * myVar; De-allocation: delete myVar;

myVar = new T;

Allocation: T * myArray; De-allocation: delete[] myArray;

Note slightly different usage for dynamically allocated variables and

arrays

(24)

IIT Bombay

Good Programming Practice

• Always de-allocate dynamically allocated memory once you have no further use of it

• Check for valid address before de-allocating memory

if (myArray != NULL) { delete[] myArray;}

preferred over

delete[] myArray;

• De-allocating memory at address 0x0 (NULL pointer) will

cause program to crash

(25)

IIT Bombay

De-allocating Dynamically Allocated Memory

Stack SegmentData Segment (Heap) 2 n

AR of main AR of

altreadQuizMarks 0x400 marks

Address 0x400

0x404 10 marks[0]

15 marks[1]

2 numStudents qMarks

0x600 temp int * altReadQuizMarks(int n)

{ int * marks, * temp, i;

marks = new int[n];

temp = new int[n];

if ((marks == NULL) || (temp == NULL)) { return NULL; }

for (i = 0; i < n; i++) { cin >> marks[i];

temp[i] = marks[i] + 10;

}

// Some computation with temp if (temp != NULL) {delete[] temp; }

(26)

IIT Bombay

De-allocating Dynamically Allocated Memory

Stack SegmentData Segment (Heap)

AR of main

Address 0x400

0x404 10 15

2 numStudents 0x400 qMarks

int main()

{ int numStudents; int *qMarks;

cout << “Given student count: “ << endl;

cin >> numStudents;

cout << “Give marks of students” << endl;

qMarks = readQuizMarks(numStudents);

// Print max and min marks from qMarks

Only persisting

dynamically allocated

memory is something

that is needed in “main”

(27)

IIT Bombay

De-allocating Dynamically Allocated Memory

Stack SegmentData Segment (Heap)

AR of main

Address 0x400

0x404 10 15

2 numStudents 0x400 qMarks

int main()

{ int numStudents; int *qMarks;

cout << “Given student count: “ << endl;

cin >> numStudents;

cout << “Give marks of students” << endl;

qMarks = readQuizMarks(numStudents);

// Print max and min marks from qMarks

“main” must now

de-allocate this memory dynamically allocated by

altReadQuizMarks

(28)

IIT Bombay

Summary

• Persistence of dynamically allocated memory

• Need for explicit de-allocation of dynamically allocated memory

• “delete” construct in C++

• Good programming practices when de-allocating dynamic

memory

References

Related documents

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

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

„ Allocation gives facts weighted assignments to possible completions, leading to an extended version of the data (Allocated Database ).. z The schema of Allocated Database

• Kernel memory may be statically allocated or kernel may ask page allocator for more pages if needed. • KMA shouldn’t suffer much from fragmentation and also should be

– Destroying the only pointer to memory allocated on the heap before it is deallocated (“Memory Leak”)... it might in general be allocated for some

Segment Classification: Target sequence consist of multiple labels and the segment locations of the input is known in advance, e.g.. the timing where each character ends and

• Our Objectives: To perform static analysis of heap allocated data for making unused data unreachable in order to improve garbage collection and plug memory leaks.. •

shmctl() allows the user to receive information on a shared memory segment, set the owner, group, and permissions of a shared memory segment, or destroy a segment... The