Download presentation
Presentation is loading. Please wait.
Published byPauline Hart Modified over 9 years ago
1
Dynamic Allocation Joe Meehean
2
Dynamic Allocation Memory for local objects is automatically created and reclaimed memory is created for it at beginning of a function memory is reclaimed at the end of the function e.g., void func(){ int a = 7; if( a == 15 ){ a *= 2; } } It would be illegal to try to reference a (use its name) outside of the function func What if we want objects that last longer? 2
3
Dynamic Allocation C++ provides a separate block of memory for this purpose called the heap or free store objects allocated (created from) the heap last until they are deallocated (explicitly returned to the heap) heap allocated objects do not have names we access them through pointers Allocating this memory is called dynamic allocation accomplished using the new operator type *pointer_name = new type; e.g., int *pInt = new int; double *pdbl = new double; 3
4
Dynamic Allocation 4 int aLocal = 21; int *pLocal = &aLocal; int *pHeap = new int; *pHeap = 34; 21 aLocal pLocal 34 pHeap
5
Dynamic Allocation Initializing dynamically allocated objects must use direct-initialization e.g., int *pInt = new int(1024); double* pDbl = new double(3.14); 5
6
Dynamic Allocation Deallocating heap objects when we are done with an object we should return its memory to the heap forgetting to deallocate heaps objects is called a memory leak memory leaks cause programs to grow until they consume all of the machines memory and crash delete operator deallocates heap objects delete pointer_name; e.g., int *pInt = new int(15);... delete pInt; 6
7
Dynamic Allocation What happens to pInt after it’s object has be deallocated? pInt becomes undefined, it is no longer valid pInt points to memory is no longer owns called a dangling pointer Cleaning up dangling pointers dangling pointers cause all sorts of programming errors ALWAYS set a pointer to 0 (or NULL) after deallocating e.g., int *pInt = new int(15);... delete pInt; pInt = NULL; 7
8
Dynamic Allocation Calling delete on a zero-valued pointer is legal and safe this is good in case you accidentally delete the same pointer twice e.g., int *pInt = new int(15);... delete pInt; pInt = NULL;... delete pInt; 8
9
Dynamic Allocation Dynamic allocation and const legal to dynamically create constant objects must be initialized when its created cannot be changed once initialized e.g., const int *pInt = new int(15); const heap objects also must be deallocated e.g., delete pInt; 9
10
Dynamic Allocation Common errors 1.Forgetting to delete dynamically allocated memory causes memory leaks 2.Reading and writing to an object after it has been deleted overwrites some other object’s memory avoid by setting pointer to 0 after deleting 3.Deleting the same memory twice two pointers may point at same heap object deleting one pointer invalidates the other calling delete on the other may corrupt the heap big time bad news 10
11
Dynamic Allocation & Arrays What if we don’t know how many elements we need in an array until after we start running? e.g., read in unknown number of items do not know correct size get number of items from user e.g., we want to make a variable size tic-tac-toe board can be 3x3, 4x4, NxN do not know until user write size on the console “Enter Board Size: “ 11
12
Dynamic Allocation & Arrays Dynamic allocation of arrays solves this problem heap can allocate memory at run time based on a variable size type *array_name = new type[dimension] returns a pointer to the first element in the array e.g., int *pIntArray = new int[15]; Must deallocate later just like other dynamic allocations syntax slightly different delete [] array_name; 12
13
Dynamic Allocation & Arrays 13 // ask user for input cout << "What is the largest prime to find?“; cout << endl; // get user input size_t max_prime; cin >> max_prime; max_prime++; // i'th entry stores whether number i is a prime bool *potential_prime = new bool[max_prime]; //run the sieve … // clean up the memory delete [] potential_prime;
14
Dynamic Allocation & Arrays Dynamic allocation of multidimensional arrays dynamically allocate an array each element of 1st array stores pointers a row row is a dynamically allocated array of columns 14 112233 445566 778899 10 11 12
15
Dynamic Allocation & Arrays Dynamic allocation of multidimensional arrays 15 int **matrix = new int*[rows]; for(int i = 0; i < rows; i++){ matrix[i] = new int[columns]; }
16
Dynamic Allocation & Arrays Dynamic allocation of multidimensional arrays 16 int count = 1; for(int i = 0; i < rows; i++){ for(int j = 0; j < columns; j++){ matrix[i][j]= count; count++; } 112233 445566 778899 10 11 12
17
Dynamic Allocation & Arrays Dynamic allocation of multidimensional arrays need to deallocate all dynamically allocated memory deallocate each row then, deallocate the array that stored row pointers 17 for(int i = 0; i < rows; i++){ delete [] matrix[i]; } delete [] matrix;
18
Questions? 18
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.