cplusplus:pointers_4_slides
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
cplusplus:pointers_4_slides [2019/03/31 22:28] – mithat | cplusplus:pointers_4_slides [2021/10/24 02:59] – [Example] mithat | ||
---|---|---|---|
Line 11: | Line 11: | ||
===== Dynamic memory allocation ===== | ===== Dynamic memory allocation ===== | ||
* **Dynamic memory allocation** allows you to reserve blocks of computer memory at // | * **Dynamic memory allocation** allows you to reserve blocks of computer memory at // | ||
- | * Typically used without associating a variable name with it. | + | * Typically used with pointers. |
===== The new operator ===== | ===== 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 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 180: | Line 184: | ||
* Memory leaks, no matter how small, are bad programming practice. | * Memory leaks, no matter how small, are bad programming practice. | ||
* 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 delete operator ===== | ||
* The '' | * The '' | ||
- | * Principle: All dynamically allocated memory must be deallocated somewhere in the program. | ||
- | * "For every '' | ||
- | <code cpp> | ||
+ | <code cpp> | ||
int *myPtr = new int; | int *myPtr = new int; | ||
... | ... | ||
- | delete myPtr; // deallocates block pointed to by localPtr. | + | delete myPtr; // deallocates block pointed to by myPtr. |
</ | </ | ||
+ | |||
+ | * Principle: All dynamically allocated memory must be deallocated somewhere in the program. | ||
+ | * "For every '' | ||
===== Example ===== | ===== Example ===== |
cplusplus/pointers_4_slides.txt · Last modified: 2021/10/24 03:02 by mithat