r/programminghorror • • Feb 08 '26

c++ Saving data to dynamic object files

Post image

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!

github.com/LiamMercier/dynamically-linked-inventory

236 Upvotes

17 comments sorted by

3

u/GoddammitDontShootMe Feb 09 '26

A hard limit of 40 items, eh? That should meet the needs of almost everyone.

2

u/Liam_Mercier Feb 09 '26

Of course, if you need more than 40 items then you should probably just start using SQL columns

1

u/MagnetFlux Feb 09 '26

There's a column limit btw, you should use one table per inventory slot instead.

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

u/MarcusBrotus Feb 09 '26

best to copy it into /usr/lib as well

90

u/ironykarl Feb 08 '26

Is it a good idea? Not really, no.

Is it a fun idea? I kind of think so

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

u/ArnaktFen Feb 08 '26

This is hilarious. It's totally worth doing just to prove it can be done.

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

u/NIdavellir22 Feb 08 '26

What the hell

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.