0

What limitations are imposed when a C module

  1. instantiates its own core struct (as a singleton) and user code accesses this using extern

VS.

  1. has user code instantiate the singleton struct instead

?

For example, one limitation of the former is that the user cannot choose the allocation method.

EDIT The reason for the question is that I have hit some walls with approach no. 1 in the past (C language limitations) but have now forgotten what they were!

Engineer
  • 8,529
  • 7
  • 65
  • 105
  • Can you show some code or explain what you mean by "instantiating singleton" in the context of C? – ci_ Sep 07 '15 at 09:39
  • @ci_ I'm trying to keep the question general. The singleton is just a given struct instance. In this case, the struct is core to the whole library, and only one such struct instance need ever exist. Question updated a little. – Engineer Sep 07 '15 at 09:45
  • Please refer this link [How to create a Singleton in C?](http://stackoverflow.com/a/803690/5184415) – venki Sep 07 '15 at 09:52
  • @venki Thanks, but unfortunately that really doesn't begin to answer the question. – Engineer Sep 07 '15 at 10:12

1 Answers1

1
  1. If you want make absolutely sure, that the singleton "stays single", let the module handle its creation.

  2. If the singleton's implementation details shall stay hidden, you also want to let the module handle it and optionally just return an opaque pointer to the singleton's internal struct, so the user has nothing more than a "handle". The handle however is not necessary as its a singleton :-).

alk
  • 69,737
  • 10
  • 105
  • 255