Advanced 14 min readModule: Module 2: Variables, Types & Memory Footprint
Data Types, sizeof & Type Casting (static_cast)
Inspect byte sizes of fundamental types with sizeof and perform safe type conversions with static_cast.
What You Will Learn in This Lesson
- Exact bit-width types (int32_t, int64_t, size_t) from <cstdint>
- Inspecting memory footprint with sizeof
- Modern C++ explicit type casting with static_cast<T>(val)
Introduction & Core Concept
In C++, every variable has an exact byte size in memory determined by the compiler and CPU architecture.
WHY DOES THIS MATTER IN THE REAL WORLD?
Using C-style casts (int)val bypasses type safety. static_cast checks conversions at compile time.
Memory Sizing & Safe Casting
cppcpp
12345678910#include <iostream>#include <cstdint>int main() {int32_t count = 42;double ratio = static_cast<double>(count) / 10.0;std::cout << "Size of int32: " << sizeof(count) << " bytes" << std::endl;std::cout << "Ratio: " << ratio << std::endl;return 0;}
Line-by-Line Technical Breakdown
1Memory alignment rules ensure variables align to CPU cache boundaries.
Try It Yourself (Interactive Editor)
Modify the code in real-time and click Run to test live browser output and console logs.
Intelligent Code Runner & Live Sandbox[CPP]
CPP SOURCE EDITOR
Interactive Live CodeIndustry Best Practices & Professional Standards
- Always prefer static_cast over C-style parenthetical casts.
Lesson Summary & Core Takeaways
- Precise byte control allows C++ systems to optimize hardware utilization.