← Back to Docs

🧠 Lazy Allocation

Memory efficiency, only when needed

Why Lazy Allocation?

The Guardian uses lazy allocation to be memory-efficient. Memory is only allocated when it's actually used.

Without Lazy Allocation

With Lazy Allocation

Example

#include <guardian/memory/lazy.hpp>

using namespace guardian::memory;

LazyStack<int> stack;

// Before: No memory allocated
std::cout << stack.size(); // 0

// After first push: Memory is allocated
stack.push(1); // 🔷 LazyStack allocated
stack.push(2);
stack.push(3);

std::cout << stack.size(); // 3

Performance Benefits

Operation Without Lazy With Lazy
Empty VM ~24 bytes 0 bytes
Startup Allocates stack No allocation