Dynamic Allocation of Memory Lecture 5 Section 9.8 Robb T. Koether Hampden-Sydney College Wed, Jan 24, 2018 Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 1 / 34
1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 2 / 34
Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 3 / 34
The Vectr Class We would like to design a class of objects that would be like arrays, but whose size could be declared at run time, not compile time. We will call this class Vectr. (There is already a vector class.) Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 4 / 34
Arrays vs. Vectrs Arrays const int MAX_SIZE = 20; int size = 10; int a[max_size]; // Legal int b[size]; // Illegal a[5] = 123; // Legal a = b; // Illegal cout << a[5] << endl; // Legal cout << a << endl; // Legal, but not advised. Why? Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 5 / 34
Arrays vs. Vectrs Vectrs const int MAX_SIZE = 20; int size = 10; Vectr v(max_size); // Legal Vectr w(size); // Legal v[5] = 123; // Legal v = w; // Legal cout << v[5] << endl; // Legal cout << v << endl; // Legal Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 6 / 34
Vectr Objects Non-empty Vectr size element 5 Empty Vectr size element 0 Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 7 / 34
Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 8 / 34
Allocation of Memory: C Style The malloc() Prototype void* malloc(int number-of-bytes); The library function malloc() allocates a specified number of bytes of memory and returns a pointer to it. Include the header file <cstdlib>. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 9 / 34
Allocation of Memory: C Style malloc() Usage int* pi = (int*)malloc(sizeof(int)); Point* ppt = (Point*)malloc(sizeof(Point)); malloc() returns a pointer to the first byte of the allocated memory block. The returned pointer is a pointer to void. The returned pointer must be cast to the proper type. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 10 / 34
Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 11 / 34
Deallocation of Memory: C Style The free() Prototype void free(void* p); The library function free() deallocates memory. Include the header file <cstdlib>. The pointer must contain an address that was previously returned by malloc(). Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 12 / 34
Deallocation of Memory: C Style The free() Usage int* p = (int*)malloc(sizeof(int)); *p = 123; // Do stuff with p. free(p); If the programmer does not call free(), then memory allocated by malloc() is automatically freed when the program exits. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 13 / 34
Dynamic Memory for Arrays: C Style Array Allocation with malloc() and free() int size = 20; int* p = (int*)malloc(size*sizeof(int)); p[5] = 123; // Do stuff with p. free(p); malloc() can be used to allocate memory for an array. Then free() will deallocate the memory. The computer remembers the size of the array. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 14 / 34
Example Example (Example) DynamicCArray.cpp. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 15 / 34
Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 16 / 34
The calloc() Function The calloc() Prototype void* calloc(int num-of-objects, int size-of-object); The library function calloc() allocates memory for a specified number of objects each of a specified size and returns a pointer to it. Include the header file <cstdlib>. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 17 / 34
The calloc() Function calloc() Usage int* pi = (int*)calloc(1, sizeof(int)); Point* ppt_arr = (Point*)calloc(50, sizeof(point)); calloc() returns a pointer to the first byte of the allocated memory block. The returned pointer is a pointer to void. The returned pointer must be cast to the proper type. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 18 / 34
The realloc() Function The realloc() Prototype void* realloc(void* p, int num-of-bytes); The library function realloc() will allocate a new block of memory containing the specified number of bytes. The contents of the old memory will be copied to the new memory (as much as fits). Include the header file <cstdlib>. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 19 / 34
The realloc() Function realloc() Usage int* p = (int*)malloc(100*sizeof(int)); for (int i = 0; i < 100; i++) p[i] = 10*i; p = (int*)realloc(p, 200*sizeof(int)); The contents 0, 10, 20,..., 990 will be copied to the new memory. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 20 / 34
Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 21 / 34
Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 22 / 34
Allocation of Memory: C++ Style The new Operator Type* p = new Type; // For single object Type* p = new Type[size]; // For an array C++ introduced the new operator to replace malloc(). It can allocate memory for a single object. And it can allocate memory for an array of objects. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 23 / 34
Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 24 / 34
Deallocation of Memory: C++ Style The delete Operator delete p; delete [] p; // Delete single object // Delete an array The delete operator will delete memory that was allocated by the new operator. delete can deallocate memory for a single object. And it can deallocate memory for an array of objects. The pointer must contain an address that was previously returned by new. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 25 / 34
Dynamic Memory for Arrays: C++ Style Array Allocation with new and delete int size = 20; int* p = new int[size]; p[5] = 123; // Do stuff with p. delete [] p; Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 26 / 34
Example Example (Example) DynamicC++Array.cpp. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 27 / 34
Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 28 / 34
Memory Leaks Definition (Memory Leak) A memory leak occurs when all pointers to a block of allocated memory have been lost. Leaked memory cannot be accessed or reallocated; it is useless. Excessive memory leaks may cause the program to run out of usable memory and crash. Memory leaks should always be avoided. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 29 / 34
Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 30 / 34
Dangling Pointers Definition (Dangling Pointer) A dangling pointer is a non-null pointer that points to unallocated memory. Dereferencing a dangling pointer may cause the program to crash. We do not necessarily avoid dangling pointers, but we must be careful. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 31 / 34
Avoiding Dangling Pointers It impossible to test a non-null pointer to see whether it is dangling. Always set pointers to NULL if they do not point to allocated memory. Then compare them to NULL to see whether they point to allocated memory. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 32 / 34
Outline 1 C-Style Memory Allocation The malloc() Function The free() Function malloc(), calloc(), and realloc() 2 C++-Style Memory Allocation The new Operator The delete Operator 3 Memory Leaks 4 Dangling Pointers 5 Assignment Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 33 / 34
Assignment Assignment Read Section 9.8. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Wed, Jan 24, 2018 34 / 34