Next Previous Contents

12. Use initialized local variables - but use it with care

Initialization of local variables when declaring them gives shorter and faster code. So, use

        int i = 1;

instead of

        int i;
        i = 1;

But beware: To maximize your savings, don't mix uninitialized and initialized variables. Create one block of initialized variables and one of uniniitalized ones. The reason for this is, that the compiler will sum up the space needed for uninitialized variables as long as possible, and then allocate the space once for all these variables. If you mix uninitialized and initialized variables, you force the compiler to allocate space for the uninitialized variables each time, it parses an initialized one. So do this:

        int i, j;
        int a = 3;
        int b = 0;

instead of

        int i;
        int a = 3;
        int j;
        int b = 0;

The latter will work, but will create larger and slower code.


Next Previous Contents