Created
June 30, 2024 20:21
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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