5

Because there is no language feature in C to protect assignment to global variables would you recommend doing something like this?

Take this example:

We have a module with the header file called module.h and implementation file module.c. The module contains a global variable called global_variable. global_variable will be updated by other functions in the implementation but we do not want users of module to be able to assign to it.

module.h:

#ifndef MODULE_H_
#define MODULE_H_

extern int global_variable;
#define global_variable (int)global_variable
/* Because the macro expansion is not an "lvalue" it cannot be assigned to. */

/* ... */

#endif

In the implementation you just #undef the macro to get full read/write access to it.

module.c:

#include "module.h"
#undef global_variable /* Make `global_variable` mutable again. */

int global_variable = 17;

/* ... */

This method obviously has the disadvantage that you cannot create a local variable with the identifier global_variable without #undefing the macro.

#include "module.h"

int f(void)
{
    int global_variable; /* This doesn't work. */
    /* ... */
}

But if you manage to come up with a naming convention that separates the identifiers this will not be a problem. Something like prefixing global variables with glbl_ or GLBL_. GLBL_ seems like a good choice since that also gives you a hint that it can be defined as a macro.

Another technique I have figured out is this:

module.h:

#ifndef MODULE_H_
#define MODULE_H_

#ifdef MODULE_IMPL_
extern int global_variable; /* Writable for the implementation. */
#else
extern const int global_variable; /* `readonly` for everyone else. */
#endif

/* ... */

#endif

module.c:

#define MODULE_IMPL_ /* I am the implementation. I want global_variable to be mutable. */
#include "module.h"

int global_variable = 17;

/* ... */

I know that the simplest and most flexible approach is to create get functions to retrieve the value of the variable at the cost of a function call. But that does not stop me from finding other hacks to solve the problem.

Are these evil things to do? I am not going to lie, I like to play with macros. But I want to know if this is something that can be applied to a serious project. :)

5
  • 13
    Using macros to create code that's syntactically identical to C but semantically different is almost always evil. Besides, you can achieve your goal better by not exposing the variable at all (make it static) and providing a function to read its value. Or better yet, don't use a global variable if you can.
    – Doval
    Jan 6, 2015 at 13:44
  • Good advice. I have learned that global state most of the time isn't the right solution for a problem and that does cause great issues with things like multi-threading. I was just theorising on different ways you can solve the problem with assignment to it by the user of the api if you were to put a decleration of it in the header. Advice taken anyways.
    – wefwefa3
    Jan 6, 2015 at 14:06
  • Related: programmers.stackexchange.com/questions/257541/…
    – Blrfl
    Jan 6, 2015 at 14:15
  • @Doval: why not make an answer from your comment? I guess you will get a lot upvotes for it.
    – Doc Brown
    Jan 6, 2015 at 15:37
  • @DocBrown Seemed a bit too trivial to be an answer, but I guess sometimes the answer really is trivial. I guess I'll go ahead and d it.
    – Doval
    Jan 6, 2015 at 15:39

1 Answer 1

12

Using macros to create code that's syntactically identical to C but semantically different is almost always evil, especially in an unsafe language like C where the fact that something compiles in no way guarantees it actually has defined behavior at run time. If you must use a macro for something, you generally want to make it clear that a macro is being used.

For this particular case you can achieve your goal better by not exposing the variable at all (i.e. make it static) and providing a function to read its value. You get the same end result (the value is read-only from outside the implementation file) but avoid using macros and their associated pitfalls altogether. Even better, don't use a global variable at all if you can; pass the value to the functions that need it.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.