Professional Documents
Culture Documents
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++
Module 10
Objectives &
Outline
Memory
Management in C
malloc & free
Memory
Management in
C++
new & delete
Array
Placement new
Restrictions
Overloading new
& delete
Module Summary
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
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
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
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
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