Dynamic Allocation of Memory Lecture 4 Sections 10.9-10.10 Robb T. Koether Hampden-Sydney College Fri, Jan 25, 2013 Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 1 / 29
1 C-Style Memory Allocation The malloc() Function The free() Function 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 Fri, Jan 25, 2013 2 / 29
Outline 1 C-Style Memory Allocation The malloc() Function The free() Function 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 Fri, Jan 25, 2013 3 / 29
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 Fri, Jan 25, 2013 4 / 29
Arrays vs. Vectrs Example (Arrays) const int cnstsize = 10; int varsize = 10; int a[cnstsize]; // Legal int b[varsize]; // Illegal a[5] = 123; // Legal a = b; // Illegal cout << a[5] << endl; // Legal cout << a << endl; // Illegal Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 5 / 29
Arrays vs. Vectrs Example (Vectrs) const int cnstsize = 10; int varsize = 10; Vectr v(cnstsize); // Legal Vectr w(varsize); // 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 Fri, Jan 25, 2013 6 / 29
Vectr Objects Non-empty Vectr size element 5 Empty Vectr size element 0 Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 7 / 29
Outline 1 C-Style Memory Allocation The malloc() Function The free() Function 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 Fri, Jan 25, 2013 8 / 29
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 Fri, Jan 25, 2013 9 / 29
Allocation of Memory: C Style malloc() Usage int* iptr = (int*)malloc(sizeof(int)); Point* pptr = (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 Fri, Jan 25, 2013 10 / 29
Outline 1 C-Style Memory Allocation The malloc() Function The free() Function 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 Fri, Jan 25, 2013 11 / 29
Deallocation of Memory: C Style The free() Prototype void free(void* ptr); 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 Fri, Jan 25, 2013 12 / 29
Deallocation of Memory: C Style The free() Usage int* iptr = (int*)malloc(sizeof(int)); *iptr = 123;. free(iptr); 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 Fri, Jan 25, 2013 13 / 29
Dynamic Memory for Arrays: C Style Array Allocation with malloc() and free() int size = 20; int* iptr = (int*)malloc(size*sizeof(int)); iptr[5] = 123;. free(iptr); 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 Fri, Jan 25, 2013 14 / 29
Example Example (Example) DynamicCArray.cpp. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 15 / 29
Outline 1 C-Style Memory Allocation The malloc() Function The free() Function 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 Fri, Jan 25, 2013 16 / 29
Outline 1 C-Style Memory Allocation The malloc() Function The free() Function 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 Fri, Jan 25, 2013 17 / 29
Allocation of Memory: C++ Style The new Operator Type* ptr = new Type; Type* ptr = new Type[size]; 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 Fri, Jan 25, 2013 18 / 29
Outline 1 C-Style Memory Allocation The malloc() Function The free() Function 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 Fri, Jan 25, 2013 19 / 29
Deallocation of Memory: C++ Style The delete Operator delete ptr; delete [] ptr; // Delete object // Delete 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 Fri, Jan 25, 2013 20 / 29
Dynamic Memory for Arrays: C++ Style Array Allocation with new and delete int size = 20; int* iptr = new int[size]; iptr[5] = 123;. delete [] iptr; Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 21 / 29
Example Example (Example) DynamicC++Array.cpp. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 22 / 29
Outline 1 C-Style Memory Allocation The malloc() Function The free() Function 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 Fri, Jan 25, 2013 23 / 29
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 Fri, Jan 25, 2013 24 / 29
Outline 1 C-Style Memory Allocation The malloc() Function The free() Function 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 Fri, Jan 25, 2013 25 / 29
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 Fri, Jan 25, 2013 26 / 29
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 Fri, Jan 25, 2013 27 / 29
Outline 1 C-Style Memory Allocation The malloc() Function The free() Function 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 Fri, Jan 25, 2013 28 / 29
Assignment Assignment Read Sections 10.9-10.10. Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013 29 / 29