Re: [cc65] declaring variables after code

From: Thomas Giesel <skoe1directbox.com>
Date: 2010-04-23 21:21:33
Hi all,

> I always try to avoid it in C code that I
> forgot that C99 actually allows it.

me too :)

to come back to the original question:

On many compilers it gives better results to declare all variables at
the beginning and not in nested blocks. This is because the stack frame
is extended and shrinked on block entries and exits. Look at this:

vars.c:
int main()
{
    int a;
    int c;
    int b = 3;

    for (a = 0; a < 3; ++a)
    {
        c = a + b;
        a = c + c;
    }
    return a;
}

vars2.c:
int main()
{
    int a;
    int b = 3;

    for (a = 0; a < 3; ++a)
    {
        int c;
        c = a + b;
        a = c + c;
    }
    return a;
}

skoe@akoya:~$ ls -la vars*
-rw-r--r-- 1 skoe skoe 1097 2010-04-23 21:18 vars
-rw-r--r-- 1 skoe skoe 1115 2010-04-23 21:15 vars2

In C the reasons to do this are rare. In C++ it may be useful if
constructors are involved, but that's a completely different language,
as Spiro told already.

Regards,

Thomas

----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Fri Apr 23 21:21:47 2010

This archive was generated by hypermail 2.1.8 : 2010-04-23 21:21:49 CEST