views:

1316

answers:

9

I have a function that is declared and defined in a header file. This is a problem all by itself. When that function is not inlined, every translation unit that uses that header gets a copy of the function, and when they are linked together there are duplicated. I "fixed" that by making the function inline, but I'm afraid that this is a fragile solution because as far as I know, the compiler doesn't guarantee inlining, even when you specify the "inline" keyword. If this is not true, please correct me.

Anyways, the real question is, what happens to static variables inside this function? How many copies do I end up with?

A: 

I believe you will end up with one per translation unit. You've effectively got many versions of that function (and its declared static variable), one for every translation unit that includes the header.

Jason Etheridge
A: 

Inlining means that executable code (instructions) is inlined into the calling function's code. The compiler can choose to do that regardless of whether you've asked it to. That has no effect on the variables (data) declared in the function.

Windows programmer
A: 

Besides any design issues this all may imply, since you're already stuck with it, you should use static in this case not inline. That way everyone shares the same variables. (Static function)

Robert Gould
+2  A: 

Since I wrote the question I tried it out with Visual Studio 2008. I tried to turn on all the options that make VS act in compliance with standards, but it's possible that I missed some. These are the results:

When the function is merely "inline", there is only one copy of the static variable.

When the function is "static inline", there are as many copies as there are translation units.

The real question is now whether things are supposed to be this way, or if this is an ideosyncracy of the Microsoft C++ compiler.

Marvin
"When the function is "static inline"," -- Your original posting didn't say anything about doing that. You should expect different results because static on a function has a different meaning from static on a variable. static on a function means other translation units won't see this definition.
Windows programmer
not sure of your settings but the compiler is working correctly in this case. However you may want to include a unit test, in case you run into some non conforming compiler down the road.
Robert Gould
+5  A: 

It is supposed to be this way. "static" tells the compiler you want the function to be local to the compilation unit, therefore you want one copy per compilation unit and one copy of the static variables per instance of the function.

"inline" used to tell the compiler you want the function to be inlined; nowadays, it just takes it as "it's ok if there are several copies of the code, just make sure it's the same function". So everybody shares the static variables.

Note: this answer was written in response to the answer the original poster posted to himself.

RaphaelSP
He is asking about 'static variables' in an 'inline function', not variables in a static function.
Richard Corden
We agree on that, but you're right: an edit is needed to put the answer back in context.
RaphaelSP
+3  A: 

I believe the compiler creates many copies of the variable, but the linker picks one and makes all the others reference it. I had similar results when I tried an experiment to create different versions of an inline function; if the function wasn't actually inlined (debug mode), all calls went to the same function regardless of the source file they were called from.

Think like a compiler for a moment - how could it be otherwise? Each compilation unit (source file) is independent of the others, and can be compiled separately; each one must therefore create a copy of the variable, thinking it is the only one. The linker has the ability to reach across those boundaries and adjust the references for both variables and functions.

P.S. If you think I'm wrong, leave a comment and let me learn something. I'm tired of anonymous downvotes.

Mark Ransom
AFAICT, you're perfectly correct in what you are saying here. I do not understand why people are down voting this answer. My only guess is that they read as far as "many copies of the variable", and then stop! :( Anyway a token (+1) from me.
Richard Corden
Awesome answer Mark..+5 if there is any provision to do :)
mahesh
+6  A: 

I guess you're missing something, here.

static?

Declaring a function static will make it "hidden" in its compilation unit. If you declare this static functions in a header, then all the compilation units units including this header will have their own copy of the function (and possibly, of the static variables inside).

This means that if you have a static function (or even a global object/variable), then this function will exist in each compilation unit (i.e. CPP file) that includes the header where this static function is declared. I have yet to see cases (but some rare debug corner cases) where this is something you want.

inline?

Declaring it inline makes it a candidate for inlining (not that it does not mean a lot nowadays in C++, as the compiler will inline or not, sometimes ignoring the fact the keyword inline is present or absent).

In a header, its has an interesting side effect: The inlined function can be defined multiple times in the same module, and the linker will simply join "them" into one (if they were not inlined for compiler's reason).

This has the advantage of "static" (i.e. it can be defined in a header) without its flaws (it exists at most once if it is not inlined)

static + inline?

Mixing inline and static will then have the consequences you described (even if the function is inlined, the static variable inside won't be, and you'll end with as much static variables as you have compilation units including the definition of your static functions).

Anyway, what's the point declaring your function static in C++?

Because it should be private? If so, don't declare it in a header (only in one CPP), or even better, use C++'s idiom for that (define it inside an anonymous namespace inside the CPP file).

(Note that I'm talking about functions, not methods: static methods have their uses in C++)

Answer to author's additionnal question

Since I wrote the question I tried it out with Visual Studio 2008. I tried to turn on all the options that make VS act in compliance with standards, but it's possible that I missed some. These are the results:

When the function is merely "inline", there is only one copy of the static variable.

When the function is "static inline", there are as many copies as there are translation units.

The real question is now whether things are supposed to be this way, or if this is an ideosyncracy of the Microsoft C++ compiler.

So I suppose you have something like that:

void doSomething()
{
   static int value ;
}

You must realise that the static variable inside the function, simply put, a global variable hidden to all but the function's scope, meaning that only the function it is declared inside can reach it.

Inlining the function won't change anything:

inline void doSomething()
{
   static int value ;
}

There will be only one hidden global variable. the fact the compiler will try to inline the code won't change the fact there is only one global hidden variable.

Now, if your function is declared static:

static void doSomething()
{
   static int value ;
}

Then it is "private" for each compilation unit, meaning that every CPP file including the header where the static function is declared will have its own private copy of the function, including its own private copy of global hidden variable, thus as much variables as there are compilation units including the header.

Adding "inline" to a "static" function with a "static" variable inside:

inline static void doSomething()
{
   static int value ;
}

has the same result than not adding this "inline" keyword, as far as the static variable inside is concerned.

So the behaviour of VC++ is correct, and you are mistaking the real meaning of "inline" and "static".

paercebal
A: 

Static means one copy is distributed throughout the program , but inline means it requires the same code for several time in the same program , so it is not possible to make a variable static inside the inline function.

+1  A: 

I found Mark Ransom's answer helpful - that the compiler creates many copies of the static variable, but the linker chooses one and enforces it across all translation units.

Elsewhere I found this:

See [dcl.fct.spec]/4:

"[..] An inline function with external linkage shall have the same address in all translation units. A static local variable in an extern inline function always refers to the same object. A string literal in an extern inline function is the same object in different translation units."

I don't have a copy of the standard to check, but it matches with my experience examining the assembly in VS Express 2008

Matt