0% found this document useful (0 votes)
6 views

Module 10

Uploaded by

yejataj548
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Module 10

Uploaded by

yejataj548
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Module M10

L
Partha Pratim
Das
Programming in Modern C++

E
Objectives &
Outline
Module M10: Dynamic Memory Management

T
Memory
Management in C
malloc & free

P
Memory
Management in
Partha Pratim Das

N
C++
new & delete
Array
Placement new Department of Computer Science and Engineering
Restrictions Indian Institute of Technology, Kharagpur
Overloading new
& delete [email protected]
Module Summary

All url’s in this module have been accessed in September, 2021 and found to be functional

Programming in Modern C++ Partha Pratim Das M10.1


Module Recap

Module M10

• Introduced operator overloading with its advantages and disadvantages

L
Partha Pratim
Das
• Explained the rules of operator overloading

E
Objectives &
Outline

T
Memory
Management in C
malloc & free

P
Memory
Management in

N
C++
new & delete
Array
Placement new
Restrictions

Overloading new
& delete

Module Summary

Programming in Modern C++ Partha Pratim Das M10.2


Module Objectives

Module M10

• Understand the dynamic memory management in C++

L
Partha Pratim
Das

E
Objectives &
Outline

T
Memory
Management in C
malloc & free

P
Memory
Management in

N
C++
new & delete
Array
Placement new
Restrictions

Overloading new
& delete

Module Summary

Programming in Modern C++ Partha Pratim Das M10.3


Module Outline

Module M10

L
Partha Pratim
Das 1 Dynamic Memory Management in C
malloc & free

E
Objectives &
Outline

T
Memory
Management in C
malloc & free
2 Dynamic Memory Management in C++

P
Memory
new and delete operator
Management in Dynamic memory allocation for Array

N
C++
new & delete Placement new
Array
Placement new Restrictions
Restrictions

Overloading new
& delete 3 Operator Overloading for Allocation and De-allocation
Module Summary

4 Module Summary

Programming in Modern C++ Partha Pratim Das M10.4


Dynamic Memory Management in C

Module M10

L
Partha Pratim
Das

E
Objectives &
Outline

T
Memory
Management in C
malloc & free

P
Memory
Management in

N
C++
new & delete
Array
Placement new
Restrictions

Overloading new
& delete

Module Summary
Dynamic Memory Management in C

Programming in Modern C++ Partha Pratim Das M10.5


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

Module M10
C Program C++ Program

L
Partha Pratim
Das #include <stdio.h> #include <iostream>
#include <stdlib.h> #include <cstdlib>

E
Objectives & using namespace std;
Outline

T
Memory int main() { int main() {
Management in C int *p = (int *)malloc(sizeof(int)); int *p = (int *)malloc(sizeof(int));
malloc & free *p = 5; *p = 5;

P
Memory
Management in printf("%d", *p); // Prints: 5 cout << *p; // Prints: 5

N
C++
new & delete
Array
free(p); free(p);
Placement new
} }
Restrictions

Overloading new
• Dynamic memory management functions in stdlib.h header for C (cstdlib header for C++)
& delete • malloc() allocates the memory on heap or free store
Module Summary
• sizeof(int) needs to be provided
• Pointer to allocated memory returned as void* – needs cast to int*
• Allocated memory is released by free() from heap or free store
• calloc() and realloc() also available in both languages

Programming in Modern C++ Partha Pratim Das M10.6


Dynamic Memory Management in C++

Module M10

L
Partha Pratim
Das

E
Objectives &
Outline

T
Memory
Management in C
malloc & free

P
Memory
Management in

N
C++
new & delete
Array
Placement new
Restrictions

Overloading new
& delete

Module Summary
Dynamic Memory Management in C++

Programming in Modern C++ Partha Pratim Das M10.7


Program 10.02/03: operator new & delete:
Dynamic memory management in C++
Module M10
• C++ introduces operators new and delete to dynamically allocate and de-allocate memory:

L
Partha Pratim
Das
Functions malloc() & free() operator new & operator delete

#include <iostream> #include <iostream>

E
Objectives &
Outline #include <cstdlib>
using namespace std; using namespace std;

T
Memory
Management in C
malloc & free int main() { int main() {

P
int *p = (int *)malloc(sizeof(int)); int *p = new int(5);
Memory
Management in *p = 5;
cout << *p; // Prints: 5 cout << *p; // Prints: 5

N
C++
new & delete
Array free(p); delete p;
Placement new } }
Restrictions

Overloading new
& delete • Function malloc() for allocation on heap • operator new for allocation on heap
Module Summary
• sizeof(int) needs to be provided • No size specification needed, type suffices
• Allocated memory returned as void* • Allocated memory returned as int*
• 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

Programming in Modern C++ Partha Pratim Das M10.8


Program 10.02/04: Functions:
operator new() & operator delete()
Module M10
• C++ also allows operator new() and operator delete() functions to dynamically allocate

L
Partha Pratim
Das and de-allocate memory:
Functions malloc() & free() Functions operator new() & operator delete()

E
Objectives &
Outline
#include <iostream> #include <iostream>

T
Memory #include <cstdlib> #include <cstdlib>
Management in C
using namespace std; using namespace std;
malloc & free

P
Memory int main() { int main() {
Management in
int *p = (int *)malloc(sizeof(int)); int *p = (int *)operator new(sizeof(int));

N
C++
new & delete *p = 5; *p = 5;
Array
Placement new cout << *p; // Prints: 5 cout << *p; // Prints: 5
Restrictions

Overloading new free(p); operator delete(p);


& 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

Programming in Modern C++ Partha Pratim Das M10.9


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

L
Partha Pratim #include <iostream> #include <iostream>
Das #include <cstdlib> using namespace std;
using namespace std;

E
Objectives &
Outline
int main() { int main() {

T
Memory int *a = (int *)malloc(sizeof(int)* 3); int *a = new int[3];
Management in C
malloc & free
a[0] = 10; a[1] = 20; a[2] = 30; a[0] = 10; a[1] = 20; a[2] = 30;

P
Memory for (int i = 0; i < 3; ++i) for (int i = 0; i < 3; ++i)
Management in
cout << "a[" << i << "] = " cout << "a[" << i << "] = "

N
C++
new & delete << a[i] << " "; << a[i] << " ";
Array
Placement new free(a); delete [] a;
Restrictions } }
Overloading new ----- -----
& delete a[0] = 10 a[1] = 20 a[2] = 30 a[0] = 10 a[1] = 20 a[2] = 30
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
Programming in Modern C++ Partha Pratim Das M10.10
Program 10.07: Operator new():
Placement new in C++
#include <iostream>
Module M10
using namespace std;
int main() { unsigned char buf[sizeof(int)* 2]; // Byte buffer on stack

L
Partha Pratim
Das // placement new in buffer buf
int *pInt = new (buf) int (3);

E
Objectives &
Outline
int *qInt = new (buf+sizeof(int)) int (5);

T
Memory int *pBuf = (int *)(buf + 0); // *pInt in buf[0] to buf[sizeof(int)-1]
Management in C
int *qBuf = (int *)(buf + sizeof(int)); // *qInt in buf[sizeof(int)] to buf[2*sizeof(int)-1]
malloc & free

P
cout << "Buf Addr Int Addr" << pBuf << " " << pInt << endl << qBuf << " " << qInt << endl;
Memory cout << "1st Int 2nd Int" << endl << *pBuf << " " << *qBuf << endl;
Management in

N
C++
new & delete int *rInt = new int(7); // heap allocation
Array cout << "Heap Addr 3rd Int" << endl << rInt << " " << *rInt << endl;
Placement new delete rInt; // delete integer from heap
Restrictions // No delete for placement new
Overloading new
}
& delete -----
• Placement operator new takes a buffer address to place objects
Buf Addr Int Addr
Module Summary • These are not dynamically allocated on heap – may be allocated on stack or heap or static,
001BFC50 001BFC50
wherever the buffer is located
001BFC54 001BFC54
• Allocations by Placement operator new must not be deleted
1st Int 2nd Int
3 5
Heap Addr 3rd Int
003799B8 7
Programming in Modern C++ Partha Pratim Das M10.11
Mixing Allocators and De-allocators of C and C++

Module M10
• Allocation and De-allocation must correctly match.

L
Partha Pratim
Das ◦ Do not free the space created by new using free()
◦ And do not use delete if memory is allocated through malloc()

E
Objectives &
Outline
These may results in memory corruption

T
Memory
Management in C
malloc & free Allocator De-allocator

P
Memory malloc() free()
Management in
operator new operator delete

N
C++
new & delete
Array
operator new[] operator delete[]
Placement new operator new() No delete
Restrictions

Overloading new
& delete • Passing NULL pointer to delete operator is secure
Module Summary • Prefer to use only new and delete in a C++ program
• 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
Programming in Modern C++ Partha Pratim Das M10.12
Operator Overloading for Allocation and De-allocation

Module M10

L
Partha Pratim
Das

E
Objectives &
Outline

T
Memory
Management in C
malloc & free

P
Memory
Management in

N
C++
new & delete
Array
Placement new
Restrictions

Overloading new
& delete

Module Summary
Operator Overloading for Allocation and De-allocation

Programming in Modern C++ Partha Pratim Das M10.13


Program 10.08: Overloading
operator new and operator delete
Module M10 #include <iostream>
#include <stdlib.h>

L
Partha Pratim using namespace std;
Das

void* operator new(size_t n) { // Definition of Operator new

E
Objectives &
Outline cout << "Overloaded new" << endl;
void *ptr = malloc(n); // Memory allocated to ptr. Can be done by function operator new()

T
Memory
Management in C return ptr;
malloc & free }

P
void operator delete(void *p) { // Definition of operator delete
Memory
Management in cout << "Overloaded delete" << endl;
free(p); // Allocated memory released. Can be done by function operator delete()

N
C++
new & delete }
Array int main() { int *p = new int; // Calling overloaded operator new
Placement new *p = 30; // Assign value to the location
Restrictions  << *p << endl;
cout << "The value is :"
Overloading new delete p; // Calling overloaded operator delete
& delete } • operator new overloaded
Module Summary ----- • The first parameter of overloaded operator new must be size t
Overloaded new • The return type of overloaded operator new must be void*
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
Programming in Modern C++ Partha Pratim Das M10.14
Program 10.09: Overloading
operator new[] and operator delete[]
Module M10 #include <iostream>
#include <cstdlib>

L
Partha Pratim using namespace std;
Das

void* operator new [] (size_t os, char setv) { // Fill the allocated array with setv

E
Objectives &
Outline void *t = operator new(os);
memset(t, setv, os);

T
Memory
Management in C
return t;
malloc & free }

P
void operator delete[] (void *ss) {
Memory
Management in
operator delete(ss);
}

N
C++
new & delete int main() {
Array char *t = new(’#’)char[10]; // Allocate array of 10 elements and fill with ’#’
Placement new
Restrictions cout << "p = " << (unsigned int) (t) << endl;
Overloading new for (int k = 0; k < 10; ++k)
& delete cout << t[k];
Module Summary
delete [] t;
} • 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
Programming in Modern C++ Partha Pratim Das M10.15
Module Summary

Module M10

• Introduced new and delete for dynamic memory management in C++

L
Partha Pratim
Das
• Understood the difference between new, new[] and delete, delete[]

E
Objectives &
Outline • Compared memory management in C with C++

T
Memory
Management in C • Explored the overloading of new, new[] and delete, delete[] operators
malloc & free

P
Memory
Management in

N
C++
new & delete
Array
Placement new
Restrictions

Overloading new
& delete

Module Summary

Programming in Modern C++ Partha Pratim Das M10.16

You might also like