Skip to content

Instantly share code, notes, and snippets.

@saxbophone
Created June 30, 2024 20:21
Show Gist options
  • Save saxbophone/b5022d162ed97862bed082d23cde7177 to your computer and use it in GitHub Desktop.
Save saxbophone/b5022d162ed97862bed082d23cde7177 to your computer and use it in GitHub Desktop.
Placement new fun! Better make sure you align them correctly! šŸ˜ˆ (for a more thorough design, might need a bookkeeping array so we can reƤllocate)
#include <array>
#include <cstdint>
#include <iostream>
#include <string>
std::byte storage[10'000];
int main() {
uint32_t* x = new(&storage[0]) uint32_t{1234};
uint64_t* y = new(&storage[8]) uint64_t{196777312};
uint16_t* z = new(&storage[16]) uint16_t{static_cast<uint16_t>(*y / *x)};
std::string* s = new(&storage[24]) std::string{"my gumfloopens"};
std::cout << *x << ", " << *y << ", " << *z << "; " << *s << "\n";
s->std::string::~string();
return *z;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment