By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,652 Members | 1,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,652 IT Pros & Developers. It's quick & easy.

linkage of static and inline functions

P: n/a
Dear Group,

The book of Bjarne Stroustrup in chapter 5.4.4 says the following

"The word static is one of the most overused words in C and C++. For
static data members it has both of the common meanings: static as in
"statically allocated" as opposed to on the stack or on the free store
and static as in "with restricted visibility" as opposed to with
external linkage. For member functions, static has the second
meaning."

In the same book in r.9.4 it says the following:

"Static members of global class have external linkage (r.3.3). The
declaration of a static data member in its class declaration is not a
definition.

AT r.3.3 it says:

"A name of file scope that is explicitly declared static is local to
its translation unit and can be uses as a name for other objects,
functions, and so on, in other translation units. Such names are said
to hae internal linkage. A name of file scope that is explicitly
declared inline is local to its translation unit..."

/** I am confused about the above mentioned statement (especially of
the phrase "A name of file scope". Can someone explain this) */

In the same section it says:

Static class members have external linkage.

1. By examining all three statements I am confused about the linkage
of static member functions as well as inline functions.

I have the following code

// File a.h
#include "stdafx.h"
#include "iostream.h"

class A {
public:
A() ;
static void someFn();
};

static void indFn();

//File a.cpp

#include "stdafx.h"
#include "a.h"
A::A() {cout << "Constuctor A"<< endl;}

void A::someFn() {cout << " print in static" << endl; }

static void indFn() {cout << " print in static ind" << endl; }

// File staticcheck.cpp

#include "stdafx.h"
#include "iostream.h"
#include "a.h"

int main(int argc, char* argv[])
{
A* a = new A;
a->someFn();
indFn();

return 0;
}

The file staticcheck.cpp only #includes a.h, however, the call to
function /* a->someFn() */ succeeds, although the definition of the
function is defined in a.cpp ( Contrary to internal linkage
directive). At the same time unless I declare and define indFn in a.h
the call to indFn() does not compile. If I declare the indFn in a.h
and define in a.cpp the code does not compile. Hence in this case it
is definitely Internal Linkage.

2. The inline functions also have internal linkage and it makes sense
to define them in each and every module. However, later discussions
give me the impression that compilers now support external linkage for
inline functions. If this is the case then it should obviate the need
to define inline function in every module, wherever it is used.

3. Lastly the static function belongs to the class and not to the
object. Hence, there is only one copy of the function available for
all threads to run. Does it mean if one thread is running the static
function, then that thread will hold a lock on that function and no
other thread can start running the code of that function unless the
other thread is done executing the static function.

4. What happens to local variables (not static variables) declared
inside static function. Do they retain their updated values across
multiple threads, or each thread gets to maintain independent values
for those variables.

Thanks

nagrik
Nov 27 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
1. Not sure what is the question here.
The static member of a class has external linkage (9.4)
In your example it is A::someFn
the static function (which is a not a member of any class ) has an
internal linkage (3.3)

you have a forward declaration of 'indFn' in a.h and the definition of
'indFn' in a.cpp.
But since it is declared status it has internal linkage and visible
only inside the translation(or compilation) unit. (It is legal to
define 'indFn' also in staticcheck.cpp).

3. >the static function belongs to the class and not to the object.
Correct. Meaning, you can call the function on class, not on the
instance
>Hence, there is only one copy of the function available for all threads to run.
Not sure what do you mean. The CODE segment is always shared for all
threads.
The static function, as any other function, can be called from several
threads simultaneously.

4. While threads share code segment, data segment and heap, each
thread has its own stack.
That's why local variable never require synchronization, only global/
static data can be accessed from several threads simultaneously.
>What happens to local variables (not static variables) declared inside static function.
They are not preserved between calls (not from the same thread, nor
from different ones)

HTH

Nov 27 '07 #2

P: n/a
nagrik wrote:
...
From your reply I follow;

1. Static member functions inside a class have external linkage. And
will have single copy of
code.
Yes.
2. Static global functinos (not belonging to any
class) have internal linkage
Yes.
and will have multiple
copies of codes with different addresses in an
application.
Well they _may_ have multiple copies. They _will_ have multiple copies
if you take explicit steps to create these copies. Like put the
function's definition into the header file and include it into several
translation units. Or manually copy the definition of the function into
several translation units.

--
Best regards,
Andrey Tarasevich
Nov 28 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.