cplusplus:pointers_4
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 [2016/03/21 23:52] – [Smart Pointers] mithat | cplusplus:pointers_4 [2019/03/31 21:45] – mithat | ||
---|---|---|---|
Line 17: | Line 17: | ||
</ | </ | ||
- | '' | + | '' |
<file cpp simple-allocation.cpp> | <file cpp simple-allocation.cpp> | ||
Line 172: | Line 172: | ||
**Deallocation** is the process of releasing back to the OS storage that was previously dynamically allocated. | **Deallocation** is the process of releasing back to the OS storage that was previously dynamically allocated. | ||
- | //Any memory that has been dynamically allocated must be deallocated | + | Deallocation of dynamically allocated |
- | Since deallocation of dynamically allocated | + | <WRAP center round tip 90%> |
+ | Any memory that you have dynamically allocated | ||
+ | </ | ||
The code below fixes the memory leak introduced above: | The code below fixes the memory leak introduced above: | ||
Line 233: | Line 235: | ||
for (count = 0; count < numDays; count++) | for (count = 0; count < numDays; count++) | ||
{ | { | ||
- | cout << "Day " << (count + 1) << ": "; | + | |
- | cin >> sales[count]; | + | cin >> sales[count]; |
} | } | ||
Line 240: | Line 242: | ||
for (count = 0; count < numDays; count++) | for (count = 0; count < numDays; count++) | ||
{ | { | ||
- | total += sales[count]; | + | |
} | } | ||
Line 261: | Line 263: | ||
==== heap vs. stack ==== | ==== heap vs. stack ==== | ||
- | Up to now, we may have given the impression that local variables and dynamically allocated storage are both allocated memory from the same amorphous | + | Up to now, we may have given the impression that local variables and dynamically allocated storage are both allocated memory from the same pool of RAM. This is not the case. The **stack** is a pool of memory whose allocation and deallocation is automatically managed. Local variables (and global ones too) are allocated from the stack. The **heap** is a pool of memory whose allocation and deallocation is explicitly managed. Dynamically allocated |
A more detailed discussion of the heap versus the stack, while important, is beyond the scope of the present discussion. But it is important to know that there are two different memory pools that C++ programs draw from. | A more detailed discussion of the heap versus the stack, while important, is beyond the scope of the present discussion. But it is important to know that there are two different memory pools that C++ programs draw from. | ||
Line 364: | Line 366: | ||
===== Smart Pointers ===== | ===== Smart Pointers ===== | ||
- | <WRAP center round todo 60%> | ||
- | TODO | ||
- | </ | ||
- | |||
C++11' | C++11' | ||
One smart pointer is the '' | One smart pointer is the '' | ||
- | + | '' | |
- | '' | + | |
<file c++ Gaddis-Pr9-17.cpp> | <file c++ Gaddis-Pr9-17.cpp> |
cplusplus/pointers_4.txt · Last modified: 2019/03/31 22:35 by mithat