Re: why static is by default initialze to zero where as auto is not
Ulrich Eckhardt wrote:
ThosRTanner wrote:
Ulrich Eckhardt wrote:
The old C way of declaring a bunch of variables at the beginning of
a block is not useful in C++ (nor in C99, btw, which allows
declaration of objects inside a block) and therefore should be
avoided anyway.
Even ANSI C allowed that (and I thought K&R did as well). I do not know
where that habit came from (?fortran), but, boy, it dies hard.
I don't think so, the following code is legal C99 but not C89:
void foo( int bar)
{
char* str = malloc(42);
if(str==NULL)
exit(2);
sprintf(str, "foo %d", bar);
int i;
for( i=0; i<strlen(str); i++)
putc(str[i]);
}
The reason is that 'i' is declared in the middle of the block and C89
doesn't allow that. Note that some compilers in fact let that slip.
Sorry, brain shutdown. I've come across a lot of places where people
are of the opinion that you can only declare things at the beginning of
a /function/ not a block. So you get code like
void func(arglist)
{
/* huge list of declarations incl a, b, c say*/
/* code that doesn't use half the declarations above. esp a, b, c */
if (something)
{
/* code that uses a, b, c*/
}
else
{
/* code that uses a and b but not c */
}
/* more code that refrains from using a, b, c */
}
the fact that a, b, and c can be declared inside the blocks comes as a
major shock to some people (and suggestions that it might be quite
dangerous, or they might need to use it outside the block one day).
Sorry - bad legacy code day
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]