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
Next revisionBoth sides next revision
cplusplus:pointers_4_slides [2019/03/31 22:31] mithatcplusplus:pointers_4_slides [2021/10/24 02:56] – [Memory management of dynamically allocated storage] mithat
Line 11: Line 11:
 ===== Dynamic memory allocation ===== ===== Dynamic memory allocation =====
   * **Dynamic memory allocation** allows you to reserve blocks of computer memory at //runtime//.   * **Dynamic memory allocation** allows you to reserve blocks of computer memory at //runtime//.
-  * Typically used without associating a variable name with it.+  * Typically used with pointers.
  
 ===== The new operator ===== ===== The new operator =====
-  * ''new <data-type>'' reserves a block of memory to hold the specified %%<data-type>%% and returns the base address of that block:+  * ''new <data-type>'' 
 +    * reserves a block of memory to hold the specified %%<data-type>%% 
 +    * returns the base address of that block
 +    * optional parenthesis around ''<data-type>''
  
 <code cpp> <code cpp>
Line 23: Line 26:
 </code> </code>
  
-  * ''foo'' points to a ''double'' that is //not// a variable.+  * ''foo'' points to a ''double'' that isn't associated with a variable identifier.
  
 ===== Example ===== ===== Example =====
Line 106: Line 109:
 void ninetynine() void ninetynine()
 { {
-    int localVar = 99;+    int localVar = 99;  // localVar is destroyed at end of fcn call
     cout << localVar << endl;     cout << localVar << endl;
 } }
Line 136: Line 139:
 void ninetynine() void ninetynine()
 { {
-    int *localPtr = new int;+    int *localPtr = nullptr;  // localPtr is destroyed at end of fcn call 
 +    localPtr = new int;       // but not dynamically allocated storage
  
     *localPtr = 99;     *localPtr = 99;
cplusplus/pointers_4_slides.txt · Last modified: 2021/10/24 03:02 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki