It's time for the summary of this module.
Sometimes we have a need to work on a collection of values and it is
not maintainable, or in some cases even possible, to store every value
in a different variable. To store an ordered collection of values in
memory under a single name, we can use arrays. This is the syntax for
array declaration. We can specify how many elements we need inside of
the square brackets, and this keyword defines the type of these
elements.
The name of the array by itself represents a memory address of the
first element from the same array. Pointer arithmetic allows us to
access other elements by using an offset.
Since this array name works kind of like a pointer to an integer
and integer values need 4 bytes, pointer arithmetic will multiply
this offset with 4. Adding one to this pointer will give us an
address of the second element,
adding two will give you the address of the third element, and so on.
Then we can use the dereference operator on that offset to access the value of
that specific element, but there is an easier way to do this.
C++ offers us the square bracket operator that will
dereference this pointer for you, you just need to provide
an offset inside of the brackets.
We can use a for loop to access all of the array elements in a uniform way.
If you have a static array,
it is usually a good idea to store its length inside of a constant
expression, so that you can change it in one place instead of hard
coding the same number all over the code.
When you pass an array to a function as an argument,
the parameter which should receive it will decay into a simple pointer.
This is why you should always pass the additional argument which contains
the number of elements in an array, because that information is lost when
we enter a local scope of a function. A function identifier is simply a
pointer to the place in memory, which holds the instructions needed to
execute that same function.
If that's the case,
then we can create our own pointers to functions, but don't
forget that these pointers have to contain the signature of a
function that they intend to point to.
We can also use legacy function pointers to pass lambdas to functions,
but only if those lambdas don't contain any captures.
Since we can't use these captures,
it is always preferred to use the standard function class from C++
instead of a simple pointer to a function. Variable length arrays
are not allowed in C++. In other words,
you cannot use a variable to define the length of an array,
because variables are created at runtime.
However, it is still possible to create arrays of dynamic size.
You just need to use the new operator for arrays to allocate them on the
heap. To prevent memory leaks, always use the complimentary delete operator
for arrays to clean up the allocated memory.
Multidimensional arrays are simply arrays which
contain other arrays as their elements.
There's a simple syntax for defining them on the stack, you just need
to put each dimension in its own square bracket.
Then, you can use the same amount of square bracket operators to define
the position of the element which you want to access. You can use the same
approach for allocating multidimensional arrays on the heap by using the
new operator for arrays. However,
this only works if only the first dimension is dynamic.
If all of the dimensions need to be dynamic,
you are better off by allocating a simple one‑dimensional array
and then use some mathematical formula to access the elements
by calculating the correct index.
In essence,
strings are just arrays of characters with the additional
null termination character at the end, which helps us with the string management.
However, since C++ provides us with standard string classes,
this legacy implementation is good to know in theory or if you are maybe
working on your own library for string management. And, we finally reached
the end of this module. Now that we covered all of the basics about heap,
pointers, and arrays, we are ready to talk about how object‑oriented
programming fits into all of this.
This is important, because OOP is one of the core pillars of
C++. Thank you for watching, and if you have any questions,
please ask them in the discussion.
I will see you in the next module.