[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

g++ and inline fns with static local variables

I've come across a problem with g++ 2.95.3 and g++ 3.2 on OpenBSD 3.2, to do with handling of inline functions with static local variables.

If an inline function has a static variable, then using the function from different compilation units should behave as though there is only one static variable. Under OpenBSD, each compilation unit that uses the inline function seems to have its own copy of the static variable.

The smallest example I can come up with involves three files:

#include <stdio.h>
inline void foo()
    static int id=0;
    printf( "foo: &id=%p\n", &id);

#include "test-inlinestatic.h"
void bar();
int main()
    return 0;

#include "test-inlinestatic.h"
void bar()

This simply calls the inline function foo() from two different compilation units. foo() prints out the address of its local static, so this address should be the same each time.

This programme can be built and run with:
g++ -o test-inlinestatic.exe test-inlinestatic1.cpp test-inlinestatic2.cpp && ./test-inlinestatic.exe

Under OpenBSD 3.2, it prints out two different values, e.g.:
foo: &id=0x20fc
foo: &id=0x2100

I've tried the same programme on two other systems (a recent cygwin and Linux 2.2.19), and they behaved correctly - e.g.:
foo: &id=0x80498d8
foo: &id=0x80498d8

Please let me know if I'm missing something.


- Julian


Visit your host, monkey.org