[prog] [C] strings bug

Akkana Peck akkana at shallowsky.com
Fri Apr 11 19:15:13 EST 2003

I'm days behind on this thread, but there you go.

Robert J. Hansen writes:
> According to C++98, globals are initialized to a value of 0 (NULL for
> pointers, 0.0 for doubles/floats) before main() is entered.  I believe
> C89 is the same way, but I wasn't able to find a definitive reference in
[ ... ]
> C is the same way:
[ proof by compiling on one machine snipped ]

Robert probably knows, but some folks may not, that compiling a program
on one machine is not a valid test of whether a feature is part of a
language standard, nor is it a good test of whether a feature is
portable.  All it tells you is how a feature works in one version
of one compiler.

Dan Richter writes:
> But let me finish by saying that depending on automatic intialization is a 
> really bad idea.

Not only that, but if you're worried about portability, do be aware that
you can't rely on all compilers to behave this way.  I've fixed lots of
bugs in projects that were caused due to programmers assuming auto-
initialization of variables, and they didn't bother to test on all the
platforms supported by the project.  Bugs like that take a lot of time
to track down.  Why not be safe, and just add the explicit "= 0"?

For some other C++ issues that can create portability problems,
see: http://www.mozilla.org/hacking/portable-cpp.html
It's a fairly old document now -- most compilers have improved,
and you may choose to adopt modern features (like STL, or omitting the
".h" from include lines) that may leave some older compilers behind.  
But make it a design choice, not an accident.


More information about the Programming mailing list