5

Is this possible?

static bool initialize()
{
  TRC_SCOPE_INIT(...); 
  ...
}

static bool initialized = initialize();

To make a very long story short, I need to call a series of macros (to initialize debugging messages) as early as possible (before thread X is started, and I don't have the ability to know when thread X is started).

7
  • 1
    Can you insert a call to pthread_once(3) in your main() routine? Commented May 21, 2012 at 1:31
  • 1
    @KingsIndian: C++ is more of a leap than C99. Commented May 21, 2012 at 1:44
  • 4
    It shouldn't be necessary to tag something C99 for it to be considered C99. That is the old standard, which is more widely available than the new C2011 standard for the next year or two. If something is only for C89, then it needs special treatment (unless the platform is tagged Windows, where the main compiler is retrograde and does not support C99 properly). Commented May 21, 2012 at 1:45
  • 1
    This is a needless argument. Adding c++ is not going to mess anything here except it will attract answers for both or better answers. Besides, it's been removed already. Commented May 21, 2012 at 1:50
  • 2
    Adding c++ tag is very harmful. It will result in getting c++ answers which are not applicable to C. Commented May 21, 2012 at 3:39

4 Answers 4

6

If you're using GCC (or clang), you can use __attribute__((constructor)):

static bool initialized = false;

__attribute__((constructor))
static void initialize(void) {
    initialized = true;
    // do some other initialization
}

int main(int argc, char **argv) {
    // initialize will have been run before main started
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

6

When I originally looked at the question, it was tagged both C and C++. The code could be C or C++ because bool is a type in C99 and C11, just as it is in C++ (almost; in C, you need the <stdbool.h> header to get the name bool).

The answers for the two tags are:

  • In C++, yes.

  • In C, no.

They are not the same language. If you need a demonstration, this is as good an example as any.

4 Comments

The question was tagged only c when it was originally posted. @KingsIndian added the c++ tag because of the bool, but I reverted that edit because bool is valid in C99 and there was no indication from the original post that it was anyhow C++ related.
@icktoofay: thanks for the clarification; I've adjusted my answer slightly to recognize the changes.
for those who are curious, (a) C++11 lets you use constexpr, (b) there is surprisingly little compiler support for it
Regardless of how the question is or was tagged, having the explanation for both the C++ and C behaviors is appropriate and makes this a better answer than having only the C answer. I think the fact that C++ readily permits this is a source of confusion for people who then want to do it in C.
1

This should be alright provided the "stuff" you are initializing is defined in the same translation unit as the call to the function that initializes it. In addition, it must be defined above the call site. Otherwise you risk undefined behavior due to use of uninitialized values.

One way to get around this is to use a pointer instead. A statically initialized pointer is compiled into the executable image, so there's no risk of undefined behavior if it's used by statics defined in other translation units. In your static initialization function you dynamically allocate the "stuff" and set the pointer to point to it so you can access it later.

Comments

1

(Since you mentioned threads, I'm going to assume you have POSIX thread functions at your disposal.)

The pthread_once function exists for this exact purpose. Anywhere you need to be sure initialize has already been called, write:

pthread_once(&init_once, initialize);

where init_once is defined with static storage duration, and possibly with external linkage if needed, as pthread_once_t init_once.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.