473,322 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

linkage of static and inline functions

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
2 3749
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
20
by: Grumble | last post by:
Hello everyone, As far as I understand, the 'inline' keyword is a hint for the compiler to consider the function in question as a candidate for inlining, yes? What happens when a function with...
10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
4
by: Peter Ammon | last post by:
I would like to share a variable between two functions defined in two separate source files; no other functions will need the global variable so I'd prefer to not give it file scope. Thus, I want...
3
by: al.cpwn | last post by:
do static and inline functions or members have internal linkage? I have been reading this newsgroup on google and found conflicting ideas. Can someone please help me understand why in some places...
13
by: fctk | last post by:
source: http://rm-f.net/~orange/devel/specifications/c89-draft.html#3.1.2.2 there are two passages in this paragraph i can't fully understand: 1) "If the declaration of an identifier for an...
1
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: -...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.