Intro to Dynamic Data
- This is chapter 14
- This is the first part 14.1.
- We have been dealing with static memory.
- All storage we currently use is allocated on the stack.
- All storage we currently use is (sort of) known at compile time.
- Think about those darned arrays.
- All storate we currnetly use is managed by the compiler.
- A second way to deal with memory is to allocate it at compile time.
- This is more flexiable, but requires more knowledge.
- This is an attention to detial thing.
- Dynamic memory is allocated at run time
- By the user
- Off the heap.
- Dynamic memory must be managed by the user
- In particular it must be freed when the user no longer needs it.
- Data
- Pionters hold pointers to a specific type of data.
- pointers are addresses, but never think of this again.
- Data types
-
datatype * identifier, * identifier, ...
-
nullptr
is a non-pointer value, like string::npos
- Operations on pointers
new datatype
returns a pointer to newly allocated memory large enough to hold an instance of the data type.
- If it is a pointer to a class, the constructor is called.
new datatype[const size_t n]
returns a pointer to newly allocated memory large enough to hold n instances of the data type.
-
delete identifier
-
delete [] i dentifier
- Assumes
identifier
is pointing to allocated data.
- Call the proper one (matches the new command)
- do not call on nullptr.
- This is an ideal way to deal with an unknown size array.