User Tools

Site Tools


cplusplus:pointers_4

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
cplusplus:pointers_4 [2017/03/20 03:35] – [Deallocation] mithatcplusplus: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 for use as storage but without associating a variable name with it. This allocation happens at runtime, hence the term //dynamic// memory allocation. It is used most often used with pointers.+**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 are most often used with pointers.
  
 ==== The ''new'' operator ==== ==== The ''new'' operator ====
Line 17: Line 17:
 </code> </code>
  
-''foo'' now points to a ''double'' that is //not// a variable.+''foo'' now points to a ''double'' that is not associated with regular variable identifier.
  
 <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. 
  
-Since deallocation of dynamically allocated storage does not happen automatically in C++, you must explicitly (i.e., manually) deallocate the memory. The ''delete'' operator lets you explicitly deallocate memory that has been dynamically allocated.+Deallocation of dynamically allocated storage does not happen automatically in C++, therefore you must explicitly (i.e., manually) deallocate the memory. The ''delete'' operator lets you explicitly deallocate memory that has been dynamically allocated.
  
 <WRAP center round tip 90%> <WRAP center round tip 90%>
-Any memory that you have dynamically allocated must be deallocated somewhere in the program, otherwise a memory leak will result.+Any memory that you have dynamically allocated must be deallocated somewhere in the program. Otherwise a memory leak will result.
 </WRAP> </WRAP>
  
Line 235: Line 235:
     for (count = 0; count < numDays; count++)     for (count = 0; count < numDays; count++)
     {     {
- cout << "Day " << (count + 1) << ": "; +        cout << "Day " << (count + 1) << ": "; 
- cin >> sales[count];+        cin >> sales[count];
     }     }
  
Line 242: Line 242:
     for (count = 0; count < numDays; count++)     for (count = 0; count < numDays; count++)
     {     {
- total += sales[count];+        total += sales[count];
     }     }
  
Line 263: 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 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 (i.e., manually) managed. Dynamically allocated memory are allocated from the heap.+Local variables and dynamically allocated storage come from different pools of RAM. 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 storage is allocated from the heap.
  
 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.
 +
 ==== ''malloc'' and ''free'' ==== ==== ''malloc'' and ''free'' ====
 ''malloc'' and ''free'' can also used be used to allocate and deallocate storage. They are part of C and so are available in C++ as well. However, they are more cumbersome than ''new'' and ''delete'' introduced in C++, and their use is discouraged. ''malloc'' and ''free'' can also used be used to allocate and deallocate storage. They are part of C and so are available in C++ as well. However, they are more cumbersome than ''new'' and ''delete'' introduced in C++, and their use is discouraged.
cplusplus/pointers_4.1489980923.txt.gz · Last modified: 2017/03/20 03:35 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki