MemViz v1.0.0
C++ Memory Layout Inspector
Loading...
Searching...
No Matches
MemViz

MemViz is a modern, header-only C++20 library for memory introspection and runtime diagnostics.
It combines compile-time reflection with runtime inspection utilities, giving developers deep visibility into how objects are laid out and managed in memory.


🌟 Why MemViz?

C++ is powerful but opaque when it comes to memory. Developers often struggle with:

  • Understanding how compilers lay out structs and classes.
  • Debugging memory leaks and tracking allocations.
  • Inspecting vtables and low-level object representations.
  • Teaching or documenting C++ memory models in an accessible way.

MemViz bridges this gap by providing transparent, lightweight tools to explore memory safely and directly.


πŸ“¦ Core Features

  • 🧩 Reflection without intrusive macros
    Register members of your types using MEMVIZ_REGISTER once, then access offsets and layout at runtime.
  • πŸ“ Layout inspection
    Print struct/class size, alignment, location, and member offsets with a single call to dumpLayout<T>().
  • πŸ›‘ Safe casting utilities
    Use castAndPrint for compile-time–checked type conversions, backed by std::bit_cast.
  • πŸŒ€ VTable exploration
    Inspect virtual function tables (inspectVTable) to understand polymorphic object layouts.
  • πŸ’§ Allocation tracking
    Catch leaks with reportLeaks and visualize current allocations with printAllocationLog.
  • ⚑️ Zero dependencies
    Header-only, no linking required. Just include and use.

πŸ’‘ Benefits

  • Debug Smarter: Detect memory leaks, unexpected allocations, and object lifetime issues early.
  • Learn by Seeing: Perfect for students and educators β€” visualize object memory layouts to understand padding, alignment, and vtables.
  • Safer Experimentation: Explore tricky areas of C++ (casting, inheritance, layout rules) without undefined behavior.
  • Production-Friendly: Minimal overhead; disable at compile-time when not needed.
  • Portable: Works across modern compilers supporting C++20.

πŸš€ Typical Use Cases

  • πŸ” Leak detection in unit tests or CI pipelines.
  • πŸ“– Teaching C++ memory model concepts (object model, padding, inheritance).
  • πŸ›  Debugging alignment and ABI issues across compilers/platforms.
  • πŸ§ͺ Experimenting with polymorphism and vtable layouts.
  • πŸ“Š Visualizing binary layouts for performance tuning or serialization design.

πŸ“˜ Example: Layout Inspection

struct Person {
int age;
char gender;
double height;
};
MEMVIZ_REGISTER(Person, std::make_tuple(
std::make_pair("age", &Person::age),
std::make_pair("gender", &Person::gender),
std::make_pair("height", &Person::height)
));
Person p = {25, 'F', 170.5};
memviz::LayoutInspector::dumpLayout(p);
````
#### Output
#define MEMVIZ_REGISTER(Type, Tuple)
Registers a user-defined type for reflection in MemViz.
Definition Macros.hpp:38

[Layout] Type: Person Size: 16 bytes Alignment: 8 bytes Location: 0x7ffee7... Members: age: offset = 0 gender: offset = 4 height: offset = 8

---
## πŸ“˜ Example: Leak Tracking

cpp Foo* a = new Foo(1, "Alpha"); Foo* b = new Foo(2, "Beta"); delete a;

memviz::reportLeaks(); // shows only b as leaked delete b;

memviz::reportLeaks(); // shows 0 leaks

#### Output

[0] LEAK 0x7fe40e705be0 (32 bytes) [SUMMARY] 1 potential leak(s), total 32 bytes. [SUMMARY] 0 potential leak(s), total 0 bytes. ```


πŸ”— Roadmap

  • πŸ“¦ CMake integration for easy consumption.
  • 🎨 Richer terminal visualization (color-coding, tree views).
  • πŸ“ JSON/CSV output for automated analysis.
  • πŸ” Optional stack trace capture for allocations.

πŸ“‚ Resources

⚑️ Summary

MemViz gives you X-ray vision into C++ memory:

  • Understand layouts.
  • Track allocations.
  • Inspect vtables.
  • Learn faster.
  • Debug smarter.

All without leaving modern, standard C++.