๐งญ Topic: Arrays
Quick Overview
๐ Covered in This Topic
- Introduction to arrays
- What arrays are at a conceptual level
- Why we need them for grouped storage and efficient access
- Arrays as contiguous blocks of memory
- Why array indices start at zero
- Pointer + offset reasoning
- Historical and implementation background
- Variable Length Arrays (VLAs)
- What they are
- Why they were removed from standard C++
- Summary of compiler behavior and portability issues
- Declaring static arrays
- Fixed-size nature
- Examples of valid declarations
- Initialization techniques
- Default initialization
- Partial initialization
- Aggregate initialization
- Examples:
int arr0[4] {};int arr1[4] {1};
- Accessing elements
- Subscript operator usage
- Input/output rules
- Why the following fail:
arr0 = arr1;cin >> firstArr;cout << firstArr;
- Index out-of-bounds and undefined behavior
- No boundary checking in C++
- Overwriting other memory
- Dangerous cases and debugging difficulty
- How arrays are stored in memory
- Contiguous memory layout
- Relationship to pointers
- Implicit decay to pointer in expressions
- Using
sizeofwith arrays- Getting total size in bytes
- Getting number of elements
- Why
sizeofgives different results inside functions
- Multidimensional arrays
- Declaration of 2D and 3D arrays
- Row-major order
- Access patterns and common mistakes
- Passing arrays to functions
- Array-to-pointer decay
- Why arrays behave like pass-by-reference
- Why arrays cannot be returned as raw types
- Correct alternatives for returning array-like structures
- Returning arrays from functions (workarounds)
- Returning pointers(will be covered later)
- Returning structs
- Using
std::arrayorstd::vector(future topic)
- Range-based for (
foreach) loops with arrays- Syntax
- Under-the-hood mechanics
- When it can and cannot be used
- Teaser for dynamic arrays
- Limitations of static arrays
- Motivation for
new[],delete[] - Transition toward
std::vectorand dynamic memory in later topics
- C-style strings
- Character arrays with
'\0'termination - How they differ from
std::string - Basic operations and pitfalls
- Character arrays with
- C++
std::string- Introduction to the
std::stringclass - Why strings are safer than C-style arrays
- Common operations:
.length()/.size().empty().substr().find().compare().append()/+concatenationoperator[]for access
- Input/output with strings
- Mutability and memory handling differences compared to arrays
- Introduction to the
๐ Slides & Materials
- ๐จโ๐ซ Professor Slides (PDF)
- ๐งโ๐ซ TA Workshop Slides (PDF)
๐ ๏ธ Workshop & Assignments
- ๐ฌ Workshop Questions
- ๐งฎ Assignments
- โ Q&A and Common Issues
๐ Additional Resources
โฉ Navigation
- โฌ ๏ธ Previous Topic: Functions 1
- โก๏ธ Next Topic: Functions 2
Tip
If something doesnโt make sense โ donโt stay stuck alone. Ask the TA to clarify it right away. Often, a quick question can save you a lot of time and frustration. Remember: if youโre confused, chances are others are too.