c99/c++ localised variable definition
Poul-Henning Kamp
phk at phk.freebsd.dk
Sat Jan 29 05:57:00 PST 2005
In message <xzpr7k4gz2x.fsf at dwp.des.no>, =?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?= writes:
>Paul Richards <paul at originative.co.uk> writes:
>> People used to programming in C++ or Perl (and many others) are used
>> to defining variables as near to use as possible. This have never been
>> possible before in C, but now with c99 it is.
>
>It's a very bad idea, because you can introduce new variables whenever
>you feel like, but you can't destroy them. Defining variables at the
>top of the scope forces you to think about which variables you need
>and how long they will live. Defining them on an ad hoc basis leads
>to sloppy programming and stack abuse.
Well... In ideal theory the compiler should reuse the stackspace
but in practice we can't rely on it happening.
I personally do like to be able to judge how much gunk a function
puts on the stack by looking at the top of the function, rather
than to have to hunt into umpteen levels for a "char buf[FAR_TOO_MUCH]".
Far too many of the cases which define local scope variables in our
current code base is bacause of lazy programmers who couldn't be
bothered to locate an unused variable amongst the ones we already
have and a minority is where the inner scope should really have
been pulled into a separate function.
--
Poul-Henning Kamp | UNIX since Zilog Zeus 3.20
phk at FreeBSD.ORG | TCP/IP since RFC 956
FreeBSD committer | BSD since 4.3-tahoe
Never attribute to malice what can adequately be explained by incompetence.
More information about the freebsd-arch
mailing list