cplusplus:pointers_4_slides
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
cplusplus:pointers_4_slides [2019/03/31 22:19] – mithat | cplusplus:pointers_4_slides [2021/10/24 03:02] – [''malloc'' and ''free''] mithat | ||
---|---|---|---|
Line 10: | Line 10: | ||
===== Dynamic memory allocation ===== | ===== Dynamic memory allocation ===== | ||
- | * **Dynamic memory allocation** allows you to reserve blocks of computer memory | + | * **Dynamic memory allocation** allows you to reserve blocks of computer memory |
- | * Happens at runtime. | + | * Typically used with pointers. |
- | ===== The '' | + | ===== The new operator ===== |
- | * '' | + | * '' |
+ | * reserves a block of memory to hold the specified %%< | ||
+ | * returns the base address of that block. | ||
+ | * optional parenthesis around ''< | ||
<code cpp> | <code cpp> | ||
Line 23: | Line 26: | ||
</ | </ | ||
- | * '' | + | * '' |
===== Example ===== | ===== Example ===== | ||
Line 36: | Line 39: | ||
double *myPtr, *yourPtr; | double *myPtr, *yourPtr; | ||
- | myPtr = new (double); | + | myPtr = new double; |
- | yourPtr = new (double); | + | yourPtr = new double; |
cout << "Enter a number: "; | cout << "Enter a number: "; | ||
Line 52: | Line 55: | ||
===== Dynamic allocation of arrays ===== | ===== Dynamic allocation of arrays ===== | ||
- | Dynamic memory allocation can be used to allocate an array: | + | * Dynamic memory allocation can be used to allocate an array. |
- | If there is not enough memory available to allocate the desired block, BadThings™ will happen. | + | * Warning: |
<file cpp dynamic-array.cpp> | <file cpp dynamic-array.cpp> | ||
Line 106: | Line 109: | ||
void ninetynine() | void ninetynine() | ||
{ | { | ||
- | int localVar = 99; | + | int localVar = 99; // localVar is destroyed at end of fcn call |
cout << localVar << endl; | cout << localVar << endl; | ||
} | } | ||
Line 136: | Line 139: | ||
void ninetynine() | void ninetynine() | ||
{ | { | ||
- | int *localPtr = new int; | + | int *localPtr = nullptr; |
+ | | ||
*localPtr = 99; | *localPtr = 99; | ||
Line 178: | Line 182: | ||
===== Memory leaks ===== | ===== Memory leaks ===== | ||
- | * Memory leaks, no matter how small, are considered | + | * Memory leaks, no matter how small, are bad programming practice. |
- | * They can be fixed by the proper use of **deallocation**: | + | * Can be fixed by the proper use of **deallocation**: |
- | * Deallocation of dynamically allocated storage does not happen automatically | + | |
- | * You must explicitly (i.e., manually) deallocate the memory. | + | * You must explicitly (i.e., manually) deallocate the memory. |
+ | |||
+ | ===== The delete operator ===== | ||
* The '' | * The '' | ||
+ | |||
+ | <code cpp> | ||
+ | int *myPtr = new int; | ||
+ | ... | ||
+ | delete myPtr; // deallocates block pointed to by myPtr. | ||
+ | </ | ||
+ | |||
* Principle: All dynamically allocated memory must be deallocated somewhere in the program. | * Principle: All dynamically allocated memory must be deallocated somewhere in the program. | ||
- | * "For every '' | + | * "For every '' |
===== Example ===== | ===== Example ===== | ||
Line 216: | Line 229: | ||
===== Deallocating arrays ===== | ===== Deallocating arrays ===== | ||
- | To deallocate dynamically allocated arrays, use square brackets: '' | + | Use square brackets to deallocate dynamically allocated arrays: '' |
<file cpp Gaddis-Pr9-14.cpp> | <file cpp Gaddis-Pr9-14.cpp> | ||
Line 228: | Line 241: | ||
int main() | int main() | ||
{ | { | ||
- | double *sales = nullptr, // To dynamically allocate an array | + | double *sales = nullptr, |
- | total = 0.0, // Accumulator | + | total = 0.0, // Accumulator |
- | average; // To hold average sales | + | average; |
- | int numDays, // To hold the number of days of sales | + | int numDays, |
- | count; // Counter variable | + | count; |
// Get the number of days of sales. | // Get the number of days of sales. | ||
Line 275: | Line 288: | ||
===== heap vs. stack ===== | ===== heap vs. stack ===== | ||
- | * Local variables and dynamically allocated storage come from different | + | * Local variables and dynamically allocated storage come from pools of RAM. |
* **stack**: a pool of memory whose allocation and deallocation is automatically managed. | * **stack**: a pool of memory whose allocation and deallocation is automatically managed. | ||
* Local and global variables are allocated from the stack. | * Local and global variables are allocated from the stack. | ||
Line 285: | Line 298: | ||
* '' | * '' | ||
* Part of C and so are available in C++ as well. | * Part of C and so are available in C++ as well. | ||
- | * More cumbersome than '' | + | * More cumbersome than '' |
===== Returning Pointers from Functions ===== | ===== Returning Pointers from Functions ===== |
cplusplus/pointers_4_slides.txt · Last modified: 2021/10/24 03:02 by mithat