r/cprogramming • u/DerpKidSavage • 1d ago
Revealed vs. Opaque structs for Libraries
/r/C_Programming/comments/1y5i6p2/revealed_vs_opaque_structs_for_libraries/
2
Upvotes
r/cprogramming • u/DerpKidSavage • 1d ago
1
u/zhivago 1d ago
It just doesn't matter.
If someone wants to they can just provide their own definition of the struct.
You can get precisely the same level of security by documentation.
A comment saying that something is for internal use is enough.
When code breaks due to depending on something clearly unstable the author can't complain.
1
u/Dapper-Situation-864 1d ago
Both work, but a few things tip it for a library:
sizeof. If you ever ship it as a DLL, that bites. With #1 you can change the internals freely.SetWindowLongPtr(GWLP_USERDATA). If the user putswindow_ton the stack and it goes out of scope, or they copy it by value, while the HWND is still alive, the WndProc is holding a dangling pointer. That alone pushes me toward #1, or at least toward documenting that awindow_tmust never be moved or copied.free().size_t window_size(void);plusint window_init(void *mem, ...)so they can place it wherever they like.Personally I'd go opaque here. Windows are few and long-lived, so the heap allocation cost is irrelevant, and you keep the freedom to change the struct without breaking users.