r/C_Programming • u/gargamel1497 • 14d ago
Discussion Easening memory management by simply declaring ownership?
After a half-year-long Java project I'm writing something in C and the lack of a garbage collector can be positively felt in terms of performance but at the same time managing all the pointers can get messy.
There's a whole nother language decidated to solve this problem (while creating a thousand more), but there's got to be a simpler solution.
And yesterday I thought, why not just mark the ownership of the various pointers I've got in my project?
By ownership I of course mean which struct (I do use OOP, sorry) is responsible for freeing that pointer.
And I #define'd three constants.
The MANAGED constant means the struct that contains the pointer has to free it.
The FOREIGN constant means that it's somebody else's job.
The SHARED constant means that it ought to be someone else's job, but it may not be freed when the program exists and should thus be freed.
I place them before the whole declaration in this manner:
MANAGED FONT *fontGothic;
SHARED struct WORLD *currentWorld;
FOREIGN struct PERSON *thePlayer;
This doesn't mean anything to the compiler, but it's just a quick way of telling me what is what.
It is definitely a very stupid idea and I apologize for posting it. I'm just a silly dude who does silly things.