This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Global variables - was: Re: [PATCH proposal] Re: Setup use on win2k - a me too ...


Larry Hall wrote:
> At 12:06 PM 11/6/2001, Pavel Tsekov wrote:
> >Christopher Faylor wrote:
> > > 
> > > It is not a bad habit to rely on global variables being 0.  If they
> > > aren't then something is seriously wrong with either 'ld' or the
> > > run-time the loader.
> >
> > I didn't know this actually, so I thought it may the cause for the
> > problem. ... However the MSVC compiled programs do not have globals,
> > except the static ones, initialized to zero ...
> 
> Chris' statement about zeroing out globals is important in it's 
> scope but shouldn't be taken too far.  Obviously, to be portable,
> adding the explicit initialization to 0 is important.  Although gcc/ld
> may not require this, other compiler suites may (as you pointed out) and 
> code, in general, can't assume too much about the compiler being used.  
> ...
> I just wanted to make this point so that someone
> doesn't get the wrong impression about initializing globals to 0.  And
> no apologies are necessary for not knowing about an obscure optimization
> feature of a compiler suite. :-)

In C, global variables which are not explicitly initialized MUST
be initialized as if their definition includes an assignment of
an integer constant 0. This is not an obscure feature of some
compilers, it's a fundamental feature of the C language! Any
system which doesn't do this is not a conforming C environment.
Assuming that an uninitialized global variable will behave as if
its definition includes '=0' is entirely portable.

I don't know C++, so I can't be sure it's the same. I thought I
knew of most of the situations where a conforming C program will
behave differently when compiled with a C++ compiler, and the
ones I know of are more subtle than this. I'll be amazed if C++
differs from C on such a basic point - but I'm easily amazed.

Pavel, can you provide an example of where MSVC doesn't do this
(perhaps to me privately rather than the list). I've never
experienced this bug, but I've only used MSVC for kernel-mode
development where different things may come into play.




--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]