User Tools

Site Tools


cplusplus:pointers_4_slides

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_slides [2021/10/24 02:55] – [Memory management of dynamically allocated storage] mithatcplusplus:pointers_4_slides [2021/10/24 03:02] (current) – [Pointers 4 slides] mithat
Line 7: Line 7:
 Gaddis, Tony. "Pointers." In //Starting Out with C++: From Control Structures through Objects//. 8th ed. Boston: Pearson, 2015. 495-546.))\\  Gaddis, Tony. "Pointers." In //Starting Out with C++: From Control Structures through Objects//. 8th ed. Boston: Pearson, 2015. 495-546.))\\ 
 Mithat Konar\\ Mithat Konar\\
-March 312019+October 232021
  
 ===== Dynamic memory allocation ===== ===== Dynamic memory allocation =====
Line 39: 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 109: Line 109:
 void ninetynine() void ninetynine()
 { {
-    int localVar = 99;  // at end of fcn call, localVar is destroyed+    int localVar = 99;  // localVar is destroyed at end of fcn call
     cout << localVar << endl;     cout << localVar << endl;
 } }
Line 139: Line 139:
 void ninetynine() void ninetynine()
 { {
-    int *localPtr = nullptr;  // localPtr gets destroyed at end of fcn call+    int *localPtr = nullptr;  // localPtr is destroyed at end of fcn call
     localPtr = new int;       // but not dynamically allocated storage     localPtr = new int;       // but not dynamically allocated storage
  
Line 184: 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**: releasing back to the OS storage that was previously dynamically allocated.    * Can be fixed by the proper use of **deallocation**: releasing back to the OS storage that was previously dynamically allocated. 
-  * Deallocation of dynamically allocated storage does not happen automatically.+  * **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.
  
Line 193: Line 193:
 int *myPtr = new int; int *myPtr = new int;
 ... ...
-delete myPtr; // deallocates block pointed to by localPtr.+delete myPtr; // deallocates block pointed to by myPtr.
 </code> </code>
  
Line 241: Line 241:
 int main() int main()
 { {
-    double *sales = nullptr, // To dynamically allocate an array +    double *sales = nullptr,    // To dynamically allocate an array 
-    total = 0.0, // Accumulator +    total = 0.0,                // Accumulator 
-    average; // To hold average sales +    average;                    // To hold average sales 
-    int numDays, // To hold the number of days of sales +    int numDays,                // To hold the number of days of sales 
-        count; // Counter variable+        count;                  // Counter variable
  
     // Get the number of days of sales.     // Get the number of days of sales.
Line 298: Line 298:
   * ''malloc'' and ''free'' can also used be used to allocate and deallocate storage.   * ''malloc'' and ''free'' can also used be used to allocate and deallocate storage.
   * Part of C and so are available in C++ as well.   * Part of C and so are available in C++ as well.
-  * More cumbersome than ''new'' and ''delete'', and their use is discouraged.+  * More cumbersome than ''new'' and ''delete'', so their use is discouraged.
  
 ===== Returning Pointers from Functions ===== ===== Returning Pointers from Functions =====
cplusplus/pointers_4_slides.1635044148.txt.gz · Last modified: 2021/10/24 02:55 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki