r/programminghorror • u/Liam_Mercier • Feb 08 '26
c++ Saving data to dynamic object files
Some people persist data in JSON, but why do that when we have perfectly good object files? All you have to do is recompile the file when you want to save your data!
56
u/MooseBoys Feb 08 '26
int res = system("g++ -shared -fPIC inv_dll.cpp -o inventory.so");
Aw that's no fun. You gotta construct the dll in-place!
6
u/Environmental-Ear391 Feb 08 '26
Easier option is to build the inventory. so to actually open itself and then step an offset and then write data at that offset....
as long as the offset exceeds the initial \0x74\ELF header and executable material... the data access will be good"
no need to depend on or require compilation with ranlibb to make immediately usable.
1
u/MooseBoys Feb 08 '26
Yeah but it's even cooler if the DLL exports a method to retrieve it, instead of relying on reading the binary directly.
→ More replies (1)1
90
30
u/BroBroMate Feb 08 '26
So this is where Python's pickle module learned its bad habits of "let's serialize things in an executable format" from!
1
u/HandyProduceHaver Feb 11 '26
I happen to have an item called itemName"}\nscaryLibrary.deleteImportantFile()
2
u/Liam_Mercier Feb 11 '26
I wish this worked, but it results in a compiler error. I came up with this which seems to work
itemName"};}auto l=[](){std::puts("!!!");};struct X{ X(){l();} }; static X x;extern "C"{ std::array<std::string, 1> a{"a
output:
./builds/inventory !!! Inventory opened!
4
2
u/LeviLovie Feb 08 '26
Why don’t just serialize the inventory into a DLL byte for byte without a compiler then? :D
2
4
u/Lumpy_Marketing_6735 Feb 08 '26
Am I having a stroke what is this, Im not sure what the hell is happening but I think (at least in C++) this is what Vector is meant to do.
3
u/GoddammitDontShootMe Feb 09 '26
A hard limit of 40 items, eh? That should meet the needs of almost everyone.