gcc4 and local statics

Brian Dessent brian@dessent.net
Thu May 19 00:06:00 GMT 2005

Christopher Faylor wrote:

> This stores a in a static location, initialized to 27.  It doesn't produce
> any special code for thread safety.  This actually makes some sense when
> assigning a constant to a static.  The constant isn't going to change and
> the static is always going to occupy one place in memory.

That's a good point, if a lot of these statics are initialized that way
then this might be a bunch of fuss over nothing.

> This, OTOH, did produce the type of code that you mentioned:

In fact the only way I had any clue about what was going on was after
looking at the disassembly of 'static int granularity = whatever ();'
and saying to myself, "okay what the heck is this __cxa_guard BS and
what has it got to do with this simple function call?".

> There is still the issue of real class constructors, of course.  Those
> are a little harder to track down than the use of ' static int foo =
> something;' but I wouldn't expect them to be very prevalent in cygwin
> either.

Here's a fun one:

int bar ();

foo ()
  static int i = bar ();

  printf ("rut-roh!\n");

bar ()
  static int j = foo ();

main ()
  foo ();

In gcc3, you get infinite recursion until the stack is gone and then
segv (or just crash.)

In gcc4, using the __cxa_guards in libstd++, this produces:

terminate called after throwing an instance of
  what():  N9__gnu_cxx14recursive_initE
Aborted (core dumped)


More information about the Cygwin-developers mailing list