user15483624
user15483624

Reputation:

How to ensure a global variable is explicitly set to some value at runtime?

Suppose there is a global variable defined in a C source file. The global variable is used by functions defined in the source file (but not functions in other source files). This source file does not contain main().

Is three a way to make sure the global variable must be explicitly set to some value before any function in this source file is called?

One way is to initialize this global variable to a illegal value in this source file. Then, when it is used, check whether it is legal or not. If it is illegal, then it means that it has not been initialized.

But I am not sure if this is a best way. Could anybody let me know what are the common ways to ensure a global variable are properly initialized?

Upvotes: 1

Views: 1281

Answers (3)

dbush
dbush

Reputation: 223689

Global variables (i.e. those defined at file scope) are always initialized. If an explicit initializer is given, it is initialized to that value at program startup. If there is no explicit initializer, it is implicitly set to 0 (or NULL if a pointer) at startup.

However, I suspect that you're using the term "initialized" loosely here and really mean "explicitly set to some value at runtime". If that's the case, then you would need to initialize that variable to some invalid value (i.e. one outside the range of what the application would expect), then whenever you use it check to see if it's that invalid value.

Upvotes: 1

jamesdlin
jamesdlin

Reputation: 89926

Generally you should avoid global variables.

A typical pattern to use instead is to put your global variables in a struct called a context, and then make all functions that would have been dependent on the global variables take an opaque pointer to that struct as an argument. This implicitly defines a contract with callers that they must acquire the context first, and the act of acquiring the context would involve allocate and initialize it.

For example:

typedef struct
{
    int someVariable;
} foo_context;

foo_context* init_foo_context(void)
{
    foo_context* ctx = malloc(sizeof *ctx);
    if (ctx != null)
    {
        ctx->someVariable = 42;
    }
    return ctx;
}

void free_foo_context(foo_context* ctx)
{
    free(ctx);
}

void some_function(foo_context* ctx, int someArg)
{
    assert(ctx != null);

    // ...
}

If you don't need to be so fancy because the typical problems associated with global variables won't ever be relevant to you, then you could hide initialization of your global variable behind a function that sets a flag. That would be equivalent to initializing your global variable to a sentinel value but is slightly more general since there might not always be an appropriate sentinel value to use.

static bool initialized = false;
static int someGlobal;

void initialize_foo(void)
{
  someGlobal = 42;
  initialized = true;
}

void some_function(int someArg)
{
    assert(initialized);

    // ...
}

Upvotes: 0

Simply declare and initialize the variable right after you import all the necessary header files

Upvotes: 0

Related Questions