You are on page 1of 12

Module 10

Intructors: Abir
Das and
Sourangshu
Bhattacharya
Module 10: Programming in C++
Objectives & Dynamic Memory Management
Outline

Memory
Management in C
malloc & free

Memory
Intructors: Abir Das and Sourangshu Bhattacharya
Management in
C++
new & delete Department of Computer Science and Engineering
Array Indian Institute of Technology, Kharagpur
Placement new
Restrictions {abir, sourangshu}@cse.iitkgp.ac.in
Overloading new
& delete

Module Summary Slides taken from NPTEL course on Programming in Modern C++

by Prof. Partha Pratim Das

CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 1


Module Objectives

Module 10

Intructors: Abir • Understand the dynamic memory management in C++


Das and
Sourangshu
Bhattacharya

Objectives &
Outline

Memory
Management in C
malloc & free

Memory
Management in
C++
new & delete
Array
Placement new
Restrictions

Overloading new
& delete

Module Summary

CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 2


Module Outline

Module 10

Intructors: Abir
Das and 1 Dynamic Memory Management in C
Sourangshu
Bhattacharya malloc & free
Objectives &
Outline

Memory
2 Dynamic Memory Management in C++
Management in C new and delete operator
malloc & free

Memory
Dynamic memory allocation for Array
Management in
C++
Placement new
new & delete Restrictions
Array
Placement new
Restrictions
3 Operator Overloading for Allocation and De-allocation
Overloading new
& delete

Module Summary
4 Module Summary

CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 3


Program 10.01/02: malloc() & free(): C & C++

Module 10
C Program C++ Program
Intructors: Abir
Das and #include <stdio.h> #include <iostream>
Sourangshu #include <stdlib.h> #include <cstdlib>
Bhattacharya
using namespace std;
Objectives &
Outline int main() { int main() {
int *p = (int *)malloc(sizeof(int)); int *p = (int *)malloc(sizeof(int));
Memory
Management in C
*p = 5; *p = 5;
malloc & free
printf("%d", *p); // Prints: 5 cout << *p; // Prints: 5
Memory
Management in
C++ free(p); free(p);
new & delete } }
Array
Placement new • Dynamic memory management functions in stdlib.h header for C (cstdlib header for C++)
Restrictions
• malloc() allocates the memory on heap or free store
Overloading new • sizeof(int) needs to be provided
& delete • Pointer to allocated memory returned as void* – needs cast to int*
Module Summary • Allocated memory is released by free() from heap or free store
• calloc() and realloc() also available in both languages

CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 4


Program 10.02/03: operator new & delete:
Dynamic memory management in C++
Module 10
• C++ introduces operators new and delete to dynamically allocate and de-allocate memory:
Intructors: Abir
Das and
Functions malloc() & free() operator new & operator delete
Sourangshu
Bhattacharya #include <iostream> #include <iostream>
#include <cstdlib>
Objectives & using namespace std; using namespace std;
Outline

Memory int main() { int main() {


Management in C int *p = (int *)malloc(sizeof(int)); int *p = new int(5);
malloc & free *p = 5;
Memory cout << *p; // Prints: 5 cout << *p; // Prints: 5
Management in
C++
free(p); delete p;
new & delete
} }
Array
Placement new
Restrictions
• Function malloc() for allocation on heap • operator new for allocation on heap
Overloading new • sizeof(int) needs to be provided • No size specification needed, type suffices
& delete
• Allocated memory returned as void* • Allocated memory returned as int*
Module Summary • Casting to int* needed • No casting needed
• Cannot be initialized • Can be initialized
• Function free() for de-allocation from heap • operator delete for de-allocation from heap
• Library feature – header cstdlib needed • Core language feature – no header needed

CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 5


Program 10.02/04: Functions:
operator new() & operator delete()
Module 10
• C++ also allows operator new() and operator delete() functions to dynamically allocate
Intructors: Abir
Das and and de-allocate memory:
Sourangshu
Bhattacharya Functions malloc() & free() Functions operator new() & operator delete()

Objectives & #include <iostream> #include <iostream>


Outline #include <cstdlib> #include <cstdlib>
Memory
using namespace std; using namespace std;
Management in C
malloc & free int main() { int main() {
Memory
int *p = (int *)malloc(sizeof(int)); int *p = (int *)operator new(sizeof(int));
Management in *p = 5; *p = 5;
C++
new & delete cout << *p; // Prints: 5 cout << *p; // Prints: 5
Array
Placement new
free(p); operator delete(p);
Restrictions
} }
Overloading new
& delete

Module Summary • Function malloc() for allocation on heap • Function operator new() for allocation on heap
• Function free() for de-allocation from heap • Function operator delete() for de-allocation from heap

There is a major difference between operator new and function operator new(). We explore this angle later

CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 6


Program 10.05/06: new[] & delete[]:
Dynamically managed Arrays in C++
Module 10 Functions malloc() & free() operator new[] & operator delete[]

Intructors: Abir #include <iostream> #include <iostream>


Das and #include <cstdlib> using namespace std;
Sourangshu
Bhattacharya using namespace std;

Objectives & int main() { int main() {


Outline int *a = (int *)malloc(sizeof(int)* 3); int *a = new int[3];
Memory
a[0] = 10; a[1] = 20; a[2] = 30; a[0] = 10; a[1] = 20; a[2] = 30;
Management in C
malloc & free for (int i = 0; i < 3; ++i) for (int i = 0; i < 3; ++i)
Memory
cout << "a[" << i << "] = " cout << "a[" << i << "] = "
Management in << a[i] << " "; << a[i] << " ";
C++
new & delete free(a); delete [] a;
Array
} }
Placement new
----- -----
Restrictions
a[0] = 10 a[1] = 20 a[2] = 30 a[0] = 10 a[1] = 20 a[2] = 30
Overloading new
& delete

Module Summary • Allocation by malloc() on heap • Allocation by operator new[] (different from operator
new) on heap
• # of elements implicit in size passed to malloc() • # of elements explicitly passed to operator new[]
• Release by free() from heap • Release by operator delete[] (different from operator
delete) from heap
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 7
Program 10.07: Operator new():
Placement new in C++
#include <iostream>
Module 10
using namespace std;
Intructors: Abir int main() { unsigned char buf[sizeof(int)* 2]; // Byte buffer on stack
Das and // placement new in buffer buf
Sourangshu
Bhattacharya int *pInt = new (buf) int (3);
int *qInt = new (buf+sizeof(int)) int (5);
Objectives &
Outline int *pBuf = (int *)(buf + 0); // *pInt in buf[0] to buf[sizeof(int)-1]
Memory
int *qBuf = (int *)(buf + sizeof(int)); // *qInt in buf[sizeof(int)] to buf[2*sizeof(int)-1]
Management in C cout << "Buf Addr Int Addr" << pBuf << " " << pInt << endl << qBuf << " " << qInt << endl;
malloc & free cout << "1st Int 2nd Int" << endl << *pBuf << " " << *qBuf << endl;
Memory
Management in int *rInt = new int(7); // heap allocation
C++ cout << "Heap Addr 3rd Int" << endl << rInt << " " << *rInt << endl;
new & delete delete rInt; // delete integer from heap
Array // No delete for placement new
Placement new }
Restrictions
-----
• Placement operator new takes a buffer address to place objects
Overloading new Buf Addr Int Addr
& delete • These are not dynamically allocated on heap – may be allocated on stack or heap or static,
001BFC50 001BFC50
wherever the buffer is located
Module Summary 001BFC54 001BFC54
• Allocations by Placement operator new must not be deleted
1st Int 2nd Int
3 5
Heap Addr 3rd Int
003799B8 7
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 8
Mixing Allocators and De-allocators of C and C++

Module 10
• Allocation and De-allocation must correctly match.
Intructors: Abir
Das and ◦ Do not free the space created by new using free()
Sourangshu
Bhattacharya ◦ And do not use delete if memory is allocated through malloc()
Objectives &
These may result in memory corruption
Outline

Memory Allocator De-allocator


Management in C
malloc & free malloc() free()
Memory operator new operator delete
Management in
C++ operator new[] operator delete[]
new & delete
Array
operator new() No delete
Placement new
Restrictions • Passing NULL pointer to delete operator is secure
Overloading new
& delete
• Prefer to use only new and delete in a C++ program
Module Summary • The new operator allocates exact amount of memory from Heap or Free Store
• new returns the given pointer type – no need to typecast
• new, new[ ] and delete, delete[] have separate semantics
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 9
Program 10.08: Overloading
operator new and operator delete
Module 10 #include <iostream>
#include <stdlib.h>
Intructors: Abir using namespace std;
Das and
Sourangshu
Bhattacharya void* operator new(size_t n) { // Definition of Operator new
cout << "Overloaded new" << endl;
Objectives & void *ptr = malloc(n); // Memory allocated to ptr. Can be done by function operator new()
Outline return ptr;
Memory }
Management in C void operator delete(void *p) { // Definition of operator delete
malloc & free cout << "Overloaded delete" << endl;
Memory free(p); // Allocated memory released. Can be done by function operator delete()
Management in }
C++
int main() { int *p = new int; // Calling overloaded operator new
new & delete
*p = 30; // Assign value to the location
Array
 << *p << endl;
cout << "The value is :"
Placement new
Restrictions
delete p; // Calling overloaded operator delete
} • operator new overloaded
Overloading new
& delete
----- • The first parameter of overloaded operator new must be size t
Overloaded new • The return type of overloaded operator new must be void*
Module Summary The value is : 30 • The first parameter of overloaded operator delete must be void*
Overloaded delete • The return type of overloaded operator delete must be void
• More parameters may be used for overloading
• operator delete should not be overloaded (usually) with extra parameters
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 10
Program 10.09: Overloading
operator new[] and operator delete[]
Module 10 #include <iostream>
#include <cstdlib>
Intructors: Abir using namespace std;
Das and
Sourangshu
Bhattacharya void* operator new [] (size_t os, char setv) { // Fill the allocated array with setv
void *t = operator new(os);
Objectives & memset(t, setv, os);
Outline return t;
Memory }
Management in C void operator delete[] (void *ss) {
malloc & free operator delete(ss);
Memory }
Management in int main() {
C++ char *t = new(’#’)char[10]; // Allocate array of 10 elements and fill with ’#’
new & delete
Array
cout << "p = " << (unsigned int) (t) << endl;
Placement new
for (int k = 0; k < 10; ++k)
Restrictions
cout << t[k];
Overloading new
& delete
delete [] t;
Module Summary } • operator new[] overloaded with initialization
----- • The first parameter of overloaded operator new[] must be size t
p = 19421992 • The return type of overloaded operator new[] must be void*
########## • Multiple parameters may be used for overloading
• operator delete [] should not be overloaded (usually) with extra parameters
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 11
Module Summary

Module 10

Intructors: Abir • Introduced new and delete for dynamic memory management in C++
Das and
Sourangshu
Bhattacharya
• Understood the difference between new, new[] and delete, delete[]
Objectives &
• Compared memory management in C with C++
Outline
• Explored the overloading of new, new[] and delete, delete[] operators
Memory
Management in C
malloc & free

Memory
Management in
C++
new & delete
Array
Placement new
Restrictions

Overloading new
& delete

Module Summary

CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 12

You might also like