cplusplus:pointers_4
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cplusplus:pointers_4 [2017/03/20 03:34] – [Deallocation] mithat | cplusplus:pointers_4 [2019/03/31 22:35] (current) – mithat | ||
---|---|---|---|
Line 5: | Line 5: | ||
===== 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 at //runtime// and use them to store variable data. The declared blocks or memory |
==== The '' | ==== The '' | ||
Line 17: | Line 17: | ||
</ | </ | ||
- | '' | + | '' |
<file cpp simple-allocation.cpp> | <file cpp simple-allocation.cpp> | ||
Line 171: | Line 171: | ||
==== Deallocation ==== | ==== Deallocation ==== | ||
**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. | ||
+ | |||
+ | Deallocation of dynamically allocated storage does not happen automatically in C++, therefore you must explicitly (i.e., manually) deallocate the memory. The '' | ||
<WRAP center round tip 90%> | <WRAP center round tip 90%> | ||
- | Any memory that you have dynamically allocated must be deallocated | + | Any memory that you have dynamically allocated must be deallocated somewhere in the program. Otherwise |
</ | </ | ||
- | |||
- | |||
- | Since deallocation of dynamically allocated storage does not happen automatically in C++, you must explicitly (i.e., manually) deallocate the memory. The '' | ||
The code below fixes the memory leak introduced above: | The code below fixes the memory leak introduced above: | ||
Line 236: | 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 243: | Line 242: | ||
for (count = 0; count < numDays; count++) | for (count = 0; count < numDays; count++) | ||
{ | { | ||
- | total += sales[count]; | + | |
} | } | ||
Line 264: | Line 263: | ||
==== heap vs. stack ==== | ==== heap vs. stack ==== | ||
- | Up to now, we may have given the impression that local variables and dynamically allocated storage | + | Local variables and dynamically allocated storage |
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. | ||
+ | |||
==== '' | ==== '' | ||
'' | '' |
cplusplus/pointers_4.1489980841.txt.gz · Last modified: 2017/03/20 03:34 by mithat