What are the problems with creating a Singleton using an unnamed struct in C?

73 views Asked by At

I want to create a singleton struct in C and came up with this idea of using an unnamed struct for defining a global variable that we can't create another instance of it because it's struct is unnamed (therefore we have a singleton).

#ifndef __for_including_this_file_once__
#define __for_including_this_file_once__

struct { variables } singleton_strcut;

#endif // __for_including_this_file_once__

This way we can access this instance only by its name anywhere we include this .h file.

  • What problems might this create, if any (especially in the context of multi-threaded programming), and what workarounds can we have?
2

There are 2 answers

4
dbush On BEST ANSWER

This won't work if you have multiple .c files. If more than one source file includes this header, you'll end up with a multiple definition error when you reach the linker phase.

You would need to put a declaration in the header file and a definition in exactly one source file, and to do that you would need to give the struct a name.

0
John Bollinger On

I want to create a singleton struct in C

It's unclear why you want to do this. The usefulness of the Singleton pattern in object oriented languages is a matter of some controversy to begin with, and C lacks some of the features that contribute on the "pro" side of that controversy in, say, C++ or Java. An excellent alternative is "just make one". Often, "don't worry about it" is good, too.

[I] came up with this idea of using an unnamed struct for defining a global variable that we can't create another instance of it because it's struct in unnamed (therefore we have a Singleton!)

And here you have touched on one of the controversial aspects of the Singleton pattern. You propose to create a global variable, and indeed, it's hard to use a singleton instance without there being some global way to reference it.

What problems might this create, if any (especially in the context of multi-threaded programming)?

  • All the problems of global variables. These are magnified in multithreaded programs.
  • Your plan doesn't work reliably anyway. C++, soon-to-come C2X, and some implementations of earlier C specifications have typeof. That allows for creating additional instances of your structure type, despite its lack of tag or typedef alias.
  • Your particular implementation does not conform to the C language specification if more than one source file includes your header. See How do I use extern to share variables between source files? for the right way to do it.

and what workarounds can we have?

Workarounds to what? Using global variables? The normal workaround is just not to use them. Instead, use functions' arguments to convey the data they should operate on. Pass pointers where you need functions to be able to modify that data (and sometimes elsewhere), or use their return values.

Workaround to support having a type of which there can be only one instance? C does not have a way to do this.