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 [2016/03/21 22:35] – [Deallocation] mithat | cplusplus:pointers_4 [2019/03/31 22:35] (current) – mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Pointers 4 ====== | ====== Pointers 4 ====== | ||
- | Dynamic memory, memory management, pointers as return types. | + | Dynamic memory, memory management, pointers as return types.((Portions adapted from: |
- | + | Gaddis, Tony. " | |
- | <WRAP center round important 60%> | + | |
- | This content has not been vetted for C++11 compliance. | + | |
- | </WRAP> | + | |
===== 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 20: | Line 17: | ||
</ | </ | ||
- | '' | + | '' |
<file cpp simple-allocation.cpp> | <file cpp simple-allocation.cpp> | ||
Line 175: | 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 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. | ||
+ | |||
==== '' | ==== '' | ||
'' | '' | ||
Line 366: | Line 366: | ||
</ | </ | ||
+ | ===== Smart Pointers ===== | ||
+ | C++11' | ||
+ | |||
+ | One smart pointer is the '' | ||
+ | |||
+ | '' | ||
+ | |||
+ | <file c++ Gaddis-Pr9-17.cpp> | ||
+ | // This program demonstrates a unique_ptr. | ||
+ | #include < | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | // Define a unique_ptr smart pointer, pointing | ||
+ | // to a dynamically allocated int. | ||
+ | | ||
+ | |||
+ | // Assign 99 to the dynamically allocated int. | ||
+ | *ptr = 99; | ||
+ | |||
+ | // Display the value of the dynamically allocated int. | ||
+ | cout << *ptr << endl; | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | '' | ||
+ | |||
+ | Dynamically allocating a large block of managed memory is demonstrated here: | ||
+ | |||
+ | <file c++ Gaddis-Pr9-18.cpp> | ||
+ | // This program demonstrates a unique_ptr pointing | ||
+ | // to a dynamically allocated array of integers. | ||
+ | #include < | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | int max; // Max size of the array | ||
+ | |||
+ | // Get the number of values to store. | ||
+ | cout << "How many numbers do you want to enter? "; | ||
+ | cin >> max; | ||
+ | |||
+ | // Define a unique_ptr smart pointer, pointing | ||
+ | // to a dynamically allocated array of ints. | ||
+ | | ||
+ | |||
+ | // Get values for the array. | ||
+ | for (int index = 0; index < max; index++) | ||
+ | { | ||
+ | cout << "Enter an integer number: "; | ||
+ | cin >> ptr[index]; | ||
+ | } | ||
+ | |||
+ | // Display the values in the array. | ||
+ | cout << "Here are the values you entered: | ||
+ | for (int index = 0; index < max; index++) | ||
+ | cout << ptr[index] << endl; | ||
+ | |||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Other smart pointers are '' |
cplusplus/pointers_4.1458599721.txt.gz · Last modified: 2016/03/21 22:35 by mithat