C89-DP: Singleton
This one is gonna be short since there realy isn't much to it.
Example
typedef struct Foo Foo;
struct Foo
{
int x;
};
Foo* Foo_getInstance()
{
static Foo instance = { 0 };
return &instance;
}
Only thing I need to note is that it's safe to take the reference of instance
since it's stored in the static register. Besides that, the rest of the program works as expected. I could've instead opted for using extern
, but I think this looks a little cleaner as I only expose what I want to expose.
Usage
Foo* foo = Foo_getInstance();
Conclusion
It absolutely can be done, in many ways even.