1

Some coding standards mandate initializing each variable when you declare it, even if the value is meaningless and the variable will soon be over written.

For example:

main()
{
   char rx_char;
   :
   :
   rx_char = get_char();

}

My co-worker asks me to initialize rx_char, but I don't understand why. Could anybody point out the reason?

4 Answers 4

5

"Initializing each variable when you declare it, even if the value is meaningless" is a schoolbook example of a cargo cult behavior. It is a completely meaningless requirement, which should be avoided whenever possible. In many cases a meaningless initializer is not much better than a random uninitialized value. In cases when it can actually "save the day", it does so by sweeping the problem under the carpet, i.e. by hiding it. Hidden problems are always the worst. When the code has a problem in it, it is always better to have it manifest itself as quickly as possible, even through having the code to crash or behave erratically.

Additionally, such requirements can impede compiler optimizations by spamming the compiler with misleading information about the effective value lifetime associated with the variable. The optimizer can treat an uninitialized variable as non-existent, thus saving valuable resources (e.g. CPU registers). In order to acquire the same kind of knowledge about a variable initialized with a meaningless value, the optimizer has to be able to figure out that it is indeed meaningless, which is generally a more complicated task.

The same problem has to be solved by a person reading the code, which impedes readability. A person unfamiliar with the code cannot say right away whether the specific initializer is meaningful (i.e. the code below relies on the initial value) or meaningless (i.e. provided thoughtlessly by a follower of the aforementioned cargo cult).

It is worth noting also that some modern compilers can detect attempts to use uninitialized variable values at both compile-time and run-time. Such compilers issue compile-time warnings or run-time assertions (in debugging builds) when such attempt is detected. In my experience this feature is much more useful than it might appear at first sight. Meanwhile, thoughtless initialization of variables with meaningless values effectively defeats this compiler-provided safety feature and, again, hides the associated errors. It certainly does more harm than good from that point of view.

Fortunately, the problem is no longer as acute as it used to be. In the modern versions of the C language variables can be declared anywhere in the code, which significantly reduces the harmful effects of such coding standards. You no longer have to declare variables at the beginning of the block, which greatly increases the chances that by the time you are ready to declare the variable you are also ready to supply a meaningful initializer for it.

Initializers are good. But when you see that you cannot provide a meaningful initializer for a variable, it is always a better idea to attempt to reorganize the code in order to create an opportunity for a meaningful initializer, instead of giving up and just using a meaningless one. This is often possible even in pre-C99 code.

Sign up to request clarification or add additional context in comments.

1 Comment

Cargo cults are great, though. Think of all that precious cargo coming down from the sky!
2

With modern compilers often giving a warning about the use of uninitialised variables I'd favour not initialising towards some bogus value. Then, if someone accidentally uses the variable before the initialisation you'll get a warning, rather than having to spend X time debugging and finding out the error.

Comments

0

It's good practice to initialize your variables, even if it's just to set them to the types "null" equivalent. If someone else comes along and wants to edit that code, and uses rx_char before you've initialized it, they will get non-deterministic behavior. The value of rx_char would be dependent on things outside of your control. Some compilers 0 initialize variables, but you should not depend on this!

By initializing it on declaration, you're guaranteeing deterministic behavior. Which is in general, easier to debug, than having rx_char be something completely random, as it could potentially be, if you left it uninitialized.

main() {
    char rx_char;
    //Some stuff
    char your_coworkers_char = rx_char;
    //Some other stuff
    rx_char = get_char();
    putchar(your_cowowarers_char); //Potentially prints randomness!
}

This is perhaps optimal, unless you're working with super old style compilers you should be fine with this.

main() {
    //some stuff
    char rx_char = get_char();
    putchar(your_cowowarers_char); //Potentially prints randomness!
}

3 Comments

Whether it's good practice is arguable / situation dependent. Better compilers and static analyzers (if you're using them) will throw an error if you try to read an uninitialized variable (say, if the example code forgot to call get_char or called it in only one branch of a conditional). They can't do that as well when you initialize everything immediately to a "null" value (especially when that "null" value is actually in-band for your domain).
Most coding standards are situation dependent. Clearly his coworker, presumably superior, are working under situations where they have the belief that intializing your variables is good. The question was not about whether it was a good standard or not, just the rationalization of this particular standard.
If the variable is uninitialized, a compiler or other analysis tool can detect that it's used before a value has been assigned to it. Initializing it to a meaningless value makes that checking impossible, since the compiler doesn't know that the initial value is meaningless.
-1

Anybody could point out the reason?

The first person you should ask is the co-worker that asked you to do it. Her reasons may be entirely different than what we come up with.

...

Most coding standards are as much about the future as they are about what happens today.

Today, yes, it doesn't make any difference if you have char rx_char or char rx_char = 0. But in the future, what happens if someone deletes the rx_char=getchar() line? Then rx_char is a random, uninitialized value. If rx_char is 0 then you'll still have a bug, but it won't be random.

1 Comment

gcc will warn you if you use uninitialized variable. if you assign the variable will meaningless value. then compiler won't warn you.

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.