0

Should vars be initialed when declared or in setup fuction? Safe to assume compiler will initialze?

Boolean x; --- init to true/false here? Void setup() . . --- or init here? .

Void loop() If (x) --- x is unknown state if not initialized.

2 Answers 2

1

Initializing a global is appropriate if you know in advance that it should always have a given value at startup. The compiler will generate a globals image in ROM that will be copied into RAM before any of your code is executed.

Safe to assume compiler will initialze?

The compiler will initialize to 0, a global not specifically initialized by you. Let's take the case of your Boolean. '0' means 'false', right? So 'x' is pre-initialized to 'false'. But I, as the reader (or yourself, six months later) may not know whether you intended this to be initialized to 'false' (the default value) or forgot to initialize it. Specifying it to be 'false' may not have any different effect on the code, but it does inform me or your future self, that you meant it to be set 'false' at startup. Remember that your code communicates to people as well as to the compiler. Write it so you or I will not only be able to understand it, but won't be able to misunderstand it.

If you initialize it in setup(), the compiler generates code in setup() specifically to do that. If the setup() function needs to decide during startup, what the initial value of that variable should be, then this is the place to do it. Otherwise, the extra code just takes up space in Flash. It's not any more harmful than wasting those few extra bytes of code-space, but it doesn't accomplish anything more than letting the globals image do it.

Whatever you did or didn't do with 'x' at load time or in 'setup()', it will persist into 'loop()', so 'x's state isn't unknown when you get there. (If I misunderstood that part of your question, please edit it in your question.)

3
  • I wouldn't be surprised if the compiler is smart enough to see that the variable isn't read before being 'initialized' in your setup function, and skip it's own 0-initialization (that is; producing identical code, whether you initialized globally, or inside the setup function)
    – Gerben
    Commented Jun 20, 2020 at 15:51
  • I just checked, and initializing a variable inside setup creates a slightly bigger sketch.
    – Gerben
    Commented Jun 20, 2020 at 16:03
  • Presumably because setup() is just like any other function and may be called at any time. Global initialization is expected to have been done by the time any user code is executed.
    – JRobert
    Commented Jun 20, 2020 at 16:54
0

There is only one rule to follow, really:

Never read a variable before writing a defined value to it.

You don't need to write explicitly into a variable, because the C++ language distinguishes between two kinds of variables:

  • Static variables are initialized to all zero bits, if no explicit initialization is done.
  • Automatic variables are never automatically initialized. Their initial value is that of the memory cells they occupy, if no explicit initialization is done.

Static variables are defined outside any function or method, or with the keyword static.

Automatic variables are all others.

Example:

static int s1; // static, initialized to 0
int s2 = 42; // static, initialized to 42

void setup() {
    int a1; // automatic, has an unknown initial value
    static int s3; // static, initialized to 0
}

void your_function() {
    static int s4 = 23; // static, initialized to 23
    int a2 = 7; // automatic, initialized to 7 each time control flow comes here
}

All static variables are initialized exactly one time, before main() is called. Or in the case of Arduino, before setup() or any other method or function is called.


Oh, another quite important rule: Know, what you are doing, don't guess, be sure.

You might need to read the standard, or at least some good book.

If you use some rare feature, make it explicit or comment.


Additional notes:

The Arduino uses C++. This is a really different language than C.

You can think of main() as:

void main() {
    setup();
    for (;;) {
        loop();
    }
}

Your Answer

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

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