Arrays, Strings, Pointers, References, and Memory Basics
Understand fundamental memory-related concepts in C++ including arrays, C-style strings, std::string, pointers, references, and basic allocation thinking.
Inside this chapter
- Arrays and std::array Thinking
- Strings
- Pointers and Addresses
- References Versus Pointers
- Dynamic Memory Basics
- Real-World Usage Snapshot
Series navigation
Study the chapters in order for the clearest path from C++ basics to modern ownership, templates, concurrency, performance, and production-ready engineering practices. Use the navigation at the bottom to move smoothly through the full series.
Arrays and std::array Thinking
int numbers[5] = {10, 20, 30, 40, 50};
std::cout << numbers[0] << '\n';
Raw arrays still exist in C++, but modern C++ often prefers safer abstractions such as std::array or std::vector when possible.
Strings
#include <string>
std::string name = "Alice";
std::cout << name.size() << '\n';
C++ supports both C-style strings and std::string. Beginners should prioritize std::string for safety and clarity, while still understanding C-style strings for interoperability and legacy code.
Pointers and Addresses
int x = 10;
int *ptr = &x;
std::cout << *ptr << '\n';
Pointers are powerful and sometimes necessary, but they must be used carefully. C++ offers higher-level alternatives in many situations, yet pointer literacy remains essential.
References Versus Pointers
| Feature | Reference | Pointer |
|---|---|---|
| Can be null | No | Yes |
| Reassignable | No | Yes |
| Syntax | Simpler | More explicit |
Dynamic Memory Basics
C++ supports dynamic memory through new and delete, but modern C++ often prefers smart pointers and containers instead of manual memory management wherever possible.
Real-World Usage Snapshot
Understanding memory basics is critical for containers, custom data structures, APIs, performance analysis, and debugging. Even when higher-level abstractions are used, they are built on these ideas.