Please enable JavaScript.
Coggle requires JavaScript to display documents.
Dynamic Memory and Members - Coggle Diagram
Dynamic Memory and Members
Dynamic Memory Allocation
High level of flexibility
Using dynamic memory allocation can optimize the memory usage
With the optimization of memoty usage it is possible to implement dynamic data structures, such as trees, linked lists and others
Operators to allocate memory
new
A pointer to a type, creating a new object of that type, and returning the address to that object.
new operator can be used to create dynamic arrays: float j = new float[size];
delete
Releases the dynamic memory space
Uses the heap (free memory)
Dynamic data structures
Different from static data structures, the dynamic data structures can change their size while the program is running
Linked List
Characteristics
Each element contains a data store, and a pointer to the next element
Each element, except from the first and last element, has a predecessor and sucessor
Arrays have contiguous memory allocation, but with linked lists you just need to allocate memory when needed
Null pointers
It is a pointer that does not have a address in memory.
They can be used to guarantee the initialization of the pointers.
Dynamic Members
Dynamic members are objects and variables that can have variable length
Dynamic members are declared using pointers within a class
Constructors are used to initialize dynamic members, allocating them in memory
Destructors are used to ensure that the memory resources that were in use, are now free
Using default copy constructors are dangerous, because some members will have pointers to the same place in memory. With that we can have conflicts, imagine cleaning the memory allocated to the variable 'a', and 'b' also has a pointer to this space in memory, 'b' will now have a pointer to something that does not exist anymore
Using copy constructors explicitly allows us to allocate a new space in memory for this copy. Now both 'a' and 'b' have their on data allocated in diffenrent memory spaces
More about pointers
Pointers of pointers
They can be used when you have a array of pointers that have to be allocated in memory
They can be used when a array of pointers is expected as an argument of a function
Functions with variable number of arguments
Functions with variable number of arguments expect at least one or more obligatory arguments
Obligatory arguments are identified by '...'
The non-obligatory arguments are acessed via a pointer
Pointers to functions
The functions names are used as pointers to the function
The same situation happens with arrays, that the array name is a pointer to the first element of the array
Complex operators
They are a combination of operators that are used at the same time to define a variable
char *strarr[50]
This is an array of pointers of char elements
Defining types
We can use typedef or using to give names to types
It increases the readability of the code and it makes declaring complex operators faster
typedef
using
Dynamic matrices
With matrices we need to use double index representation to access an element
The first index returns a reference to an array, and the second index returns the reference to the value on the index of the given array
Constructors can be used to allocate memory for new rows of the matrix
Destructor are used to release the memory occupied by the rows at the matrix