Ch7: Arrays¶
TL;DR: Contiguous Memory Allocation: Arrays allocate sequential memory blocks for variables of the same type, accessed via zero-based index offsets.
โก Quick Reference¶
#include <iostream>
#include <iterator>
int main() {
// type name[size] - Declare an uninitialized array of fixed size.
int arr1[5];
// type name[size] = { ... } - Declare and initialize elements directly.
int arr2[5] = {10, 20, 30, 40, 50};
// type name[] = { ... } - Omit size to let compiler deduce it from initializer list.
int arr3[] = {1, 2, 3};
// sizeof(array) / sizeof(array[0]) - Computes array size (legacy C-style approach).
size_t size1 = sizeof(arr2) / sizeof(arr2[0]);
// std::size(array) - Retrieve array size via standard helper (C++17).
size_t size2 = std::size(arr2);
// type name[rows][cols] - Declare a multi-dimensional array.
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
return 0;
}
๐ง Core Concepts¶
- Contiguous Memory Allocation: Arrays allocate sequential memory blocks for variables of the same type, accessed via zero-based index offsets.
- Lack of Bounds Checking: C++ does not perform runtime or compile-time boundary checks, meaning out-of-bounds array access results in undefined behavior.
- Character Array Specialization: Character arrays are treated as C-strings if they contain a null terminator (
\0), permitting direct console input and output operations.
โ ๏ธ Pitfalls (Quick Scan)¶
| Mistake | Fix |
|---|---|
| Accessing array elements out of bounds | Always validate that indices are within the range 0 to size - 1 (Lecture 4) |
| Declaring arrays without explicit initialization | Use braced initialization ({}) to default-initialize or set explicit values (Lecture 1) |
Omitting the null terminator (\0) in character arrays |
Use double-quoted string literals or insert \0 manually at the end (Lecture 3) |
Declaring auto-sized array int arr[]; without initializing values |
Provide an initializer list or specify size within brackets (Lecture 2) |
| Attempting to print non-character arrays directly | Iterate through elements using a loop to print them individually (Lecture 3) |
| Modifying elements of a const-declared array | Remove the const modifier if elements need to be mutable (Lecture 1) |
| Declaring character array size smaller than string literal length | Allocate size equal to string length plus one for null terminator (Lecture 3) |
| Omitting non-leftmost dimensions in multi-dimensional arrays | Always specify the sizes of all outer dimensions in multi-dimensional declarations (Multi-D lecture) |
๐ Full Details¶
Cause โ Effect โ Fix with timestamp (click to expand)
* **Accessing array elements out of bounds** -> **Undefined behavior, memory corruption, or runtime application crashes** -> **Always validate that indices are within the range `0` to `size - 1` (Lecture 4)** * **Declaring arrays without explicit initialization** -> **Array contains unpredictable garbage memory values** -> **Use braced initialization (`{}`) to default-initialize or set explicit values (Lecture 1)** * **Omitting the null terminator (`\0`) in character arrays** -> **Printing displays garbage characters until a null byte is found in memory** -> **Use double-quoted string literals or insert `\0` manually at the end (Lecture 3)** * **Declaring auto-sized array `int arr[];` without initializing values** -> **Compile-time error because array size cannot be deduced** -> **Provide an initializer list or specify size within brackets (Lecture 2)** * **Attempting to print non-character arrays directly** -> **Outputs the array's memory address pointer instead of elements** -> **Iterate through elements using a loop to print them individually (Lecture 3)** * **Modifying elements of a const-declared array** -> **Compile-time error because elements are read-only** -> **Remove the `const` modifier if elements need to be mutable (Lecture 1)** * **Declaring character array size smaller than string literal length** -> **Compile-time error because initializer contains too many values (missing space for `\0`)** -> **Allocate size equal to string length plus one for null terminator (Lecture 3)** * **Omitting non-leftmost dimensions in multi-dimensional arrays** -> **Compile-time error because only the leftmost dimension can be deduced** -> **Always specify the sizes of all outer dimensions in multi-dimensional declarations (Multi-D lecture)**๐ Repo Files¶
12.Arrays/12.2DeclaringAndUsingArrays/main.cpp12.Arrays/12.3SizeOfAnArray/main.cpp12.Arrays/12.4ArraysOfCharacters/main.cpp12.Arrays/12.5ArrayBounds/main.cpp12.Arrays/12.6GeneratingRandomNumbers/main.cpp12.Arrays/12.7Practice_FortuneTellerV1/main.cpp12.Arrays/12.8MultiDimensionalArrays/main.cpp12.Arrays/12.9MultiDimensionalArraysOfCharacters/main.cpp12.Arrays/12.10Practice_FortuneTellerV2/main.cpp