I have noticed that C programmers put static keyword beside global
variable and global functions in C source codes. I believe that it is not
necessary and it is not the practice in C++. Static keyword is useful
inside struct, class, and function only unless you want to force local
variable to be global variable so static is used.
Do you have idea why most programmers do this?
Bryan Parkoff 9 6351
"Bryan Parkoff" <no****@nospam. com> writes: I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static keyword is useful inside struct, class, and function only unless you want to force local variable to be global variable so static is used. Do you have idea why most programmers do this?
To enforce internal linkage. The same thing can be achieved by putting
globals into unnamed namespace.
--
Mikhail Polatov
"Mikhail Polatov" <mp******@met a-comm.com> wrote in message
news:ur******** ***@meta-comm.com... "Bryan Parkoff" <no****@nospam. com> writes:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static keyword is useful inside struct, class, and function only unless you want to force local variable to be global variable so static is used. Do you have idea why most programmers do this?
To enforce internal linkage. The same thing can be achieved by putting globals into unnamed namespace.
Mikhail,
I have read a book what it explained internal linkage. I am not sure I
understand what internal linkage means. I always place global variables and
global variables inside namespace in C++ unlike C. Is it true that I don't
need to use static keyword beside global variables and global variables if
they are always in global scope.
Please explain what is the difference between internal linkage and
external linkage. Thanks...
Bryan Parkoff
"Bryan Parkoff" <no****@nospam. com> writes: "Mikhail Polatov" <mp******@met a-comm.com> wrote in message news:ur******** ***@meta-comm.com... "Bryan Parkoff" <no****@nospam. com> writes:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static keyword is useful inside struct, class, and function only unless you want to force local variable to be global variable so static is used. Do you have idea why most programmers do this?
To enforce internal linkage. The same thing can be achieved by putting globals into unnamed namespace.
Mikhail,
I have read a book what it explained internal linkage. I am not sure I understand what internal linkage means. I always place global variables and global variables inside namespace in C++ unlike C. Is it true that I don't need to use static keyword beside global variables and global variables if they are always in global scope. Please explain what is the difference between internal linkage and external linkage. Thanks...
3.5.2 A name is said to have linkage when it might denote the same
object, reference, function, type, template, namespace or value as a
name introduced by a declaration in another scope:
- When a name has external linkage, the entity it denotes can be
referred to by names from scopes of other translation units or from
other scopes of the same translation unit.
- When a name has internal linkage, the entity it denotes can be
referred to by names from other scopes in the same translation unit.
- When a name has no linkage, the entity it denotes cannot be referred
to by names from other scopes.
--
Mikhail Polatov
"Mikhail Polatov" <mp******@met a-comm.com> wrote in message
news:um******** ***@meta-comm.com... "Bryan Parkoff" <no****@nospam. com> writes:
"Mikhail Polatov" <mp******@met a-comm.com> wrote in message news:ur******** ***@meta-comm.com... "Bryan Parkoff" <no****@nospam. com> writes:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static keyword is useful inside struct, class, and function only unless you want to force local variable to be global variable so static is used. Do you have idea why most programmers do this?
To enforce internal linkage. The same thing can be achieved by putting globals into unnamed namespace.
Mikhail,
I have read a book what it explained internal linkage. I am not sure I understand what internal linkage means. I always place global variables and global variables inside namespace in C++ unlike C. Is it true that I don't need to use static keyword beside global variables and global variables if they are always in global scope. Please explain what is the difference between internal linkage and external linkage. Thanks...
3.5.2 A name is said to have linkage when it might denote the same object, reference, function, type, template, namespace or value as a name introduced by a declaration in another scope: - When a name has external linkage, the entity it denotes can be referred to by names from scopes of other translation units or from other scopes of the same translation unit. - When a name has internal linkage, the entity it denotes can be referred to by names from other scopes in the same translation unit. - When a name has no linkage, the entity it denotes cannot be referred to by names from other scopes.
Mikhail,
Thank you for explaining the difference between internal linkage and
external linkage. I still don't understand your comment. I think that
external linkage means to call variables or functions from first file to
another second file. Internal linkage means to bind all local variables and
local functions inside struct, class, or namespace. Also, another meaning
is to hold global variables and global functions inside one file which they
are inaccessible to other files.
For Example:
// First_File.h
#ifndef FIRST_FILE.H
#define FIRST_FILE.H
extern int Obj1;
extern int Obj2;
extern int Obj3;
extern void Read_Obj(void);
#endif
// First_File.cpp
#include "First_File .h"
#include <stdio.h>
int Obj1 = 0;
int Obj2 = 0;
int Obj3 = 0;
void Read_Obj(void)
{
for (int a = 0; a < 10; ++a)
{
Obj1 += 1;
Obj2 += 2;
Obj3 += 3;
}
printf("Obj1: %d\nObj2: %d\nObj3: %d\n", Obj1, Obj2, Obj3);
}
// Second_File.cpp
#include "First_File .h"
int main(void)
{
Read_Obj();
return 0;
}
My example source code above is external linkage. Please give me your
example source which it is the internal linkage including static. I always
add namespace each header files and source code files to prevent overwritten
existing global variables according to my choice, but I can use struct or
class if I want on depend of my design.
The problem is that Read_Obj() function can't be forced inline. It is
not the C++ Compiler's decision to grant or deny __forceinline because
Read_Obj() function has external linkage. It creates two object files such
as *.obj. It is impossible to copy Read_Obj() function from first *.obj to
second *.obj which it pastes into main() function.
If Read_Obj() function is placed in Second_File.cpp rather than
First_File.cpp, __forceinline will work while C++ Compiler can always grant
or deny __forceinline because Read_Obj() function has internal linkage.
I used three different C++ Compiler such as Microsoft Visual C++, Intel
C++ Compiler, and GCC Compiler, but their results are always the same.
Is my concept explanation correct?
Bryan Parkoff
"Bryan Parkoff" <no****@nospam. com> writes: "Mikhail Polatov" <mp******@met a-comm.com> wrote in message news:um******** ***@meta-comm.com... "Bryan Parkoff" <no****@nospam. com> writes:
"Mikhail Polatov" <mp******@met a-comm.com> wrote in message news:ur******** ***@meta-comm.com... "Bryan Parkoff" <no****@nospam. com> writes:
> I have noticed that C programmers put static keyword beside global > variable and global functions in C source codes. I believe that it is > not > necessary and it is not the practice in C++. Static keyword is useful > inside struct, class, and function only unless you want to force local > variable to be global variable so static is used. > Do you have idea why most programmers do this?
To enforce internal linkage. The same thing can be achieved by putting globals into unnamed namespace.
Mikhail,
I have read a book what it explained internal linkage. I am not sure I understand what internal linkage means. I always place global variables and global variables inside namespace in C++ unlike C. Is it true that I don't need to use static keyword beside global variables and global variables if they are always in global scope. Please explain what is the difference between internal linkage and external linkage. Thanks... 3.5.2 A name is said to have linkage when it might denote the same object, reference, function, type, template, namespace or value as a name introduced by a declaration in another scope: - When a name has external linkage, the entity it denotes can be referred to by names from scopes of other translation units or from other scopes of the same translation unit. - When a name has internal linkage, the entity it denotes can be referred to by names from other scopes in the same translation unit. - When a name has no linkage, the entity it denotes cannot be referred to by names from other scopes.
Mikhail,
Thank you for explaining the difference between internal linkage and external linkage. I still don't understand your comment. I think that external linkage means to call variables or functions from first file to another second file. Internal linkage means to bind all local variables and local functions inside struct, class, or namespace. Also, another meaning is to hold global variables and global functions inside one file which they are inaccessible to other files.
For Example:
// First_File.h #ifndef FIRST_FILE.H #define FIRST_FILE.H
extern int Obj1; extern int Obj2; extern int Obj3;
External linkage, extern modifier. extern void Read_Obj(void);
The same.
#endif
// First_File.cpp #include "First_File .h" #include <stdio.h>
int Obj1 = 0; int Obj2 = 0; int Obj3 = 0;
External linkage by default. void Read_Obj(void) { for (int a = 0; a < 10; ++a) { Obj1 += 1; Obj2 += 2; Obj3 += 3; }
printf("Obj1: %d\nObj2: %d\nObj3: %d\n", Obj1, Obj2, Obj3); }
// Second_File.cpp #include "First_File .h"
int main(void) { Read_Obj();
return 0; }
My example source code above is external linkage. Please give me your example source which it is the internal linkage including static. I always add namespace each header files and source code files to prevent overwritten existing global variables according to my choice, but I can use struct or class if I want on depend of my design.
// internal linkage
static int obj1 = 0; // way 1: use static modifier
int const obj2 = 0; // way 2: use const without extern modifier
static void foo();
namespace {
int obj3 = 0; // way 3: use unnamed namespace
void foo2();
}
The problem is that Read_Obj() function can't be forced inline. It is not the C++ Compiler's decision to grant or deny __forceinline because Read_Obj() function has external linkage. It creates two object files such as *.obj.
The __forceinline keyword is MSVC specific compiler extension. It has
the same semantics as inline has, but while the latter is just a hint
to the compiler, the former forces the compiler to do inlining (under
some conditions).
IHMO, in this example the function Read_Obj is not a good candidate
for inlining anyway.
It is impossible to copy Read_Obj() function from first *.obj to second *.obj which it pastes into main() function.
And what for? Those object files may end up in different
programs/libraries. Don't forget about ODR (One Definition Rule).
If Read_Obj() function is placed in Second_File.cpp rather than First_File.cpp, __forceinline will work while C++ Compiler can always grant or deny __forceinline because Read_Obj() function has internal linkage.
The compiler does compile translation units one at a time, and it
doesn't resolve any references between translation units, but a linker
does. And it's too late for the linker to any inlining since all code
have already been generated.
And as mentioned above, __forceinline does force inlining without any
cost/performance analysis, but it makes sense for functions with
internal linkage only.
--
Mikhail Polatov
"Mikhail Polatov" <mp******@met a-comm.com> wrote in message
news:ui******** ***@meta-comm.com... "Bryan Parkoff" <no****@nospam. com> writes:
"Mikhail Polatov" <mp******@met a-comm.com> wrote in message news:um******** ***@meta-comm.com... "Bryan Parkoff" <no****@nospam. com> writes:
"Mikhail Polatov" <mp******@met a-comm.com> wrote in message news:ur******** ***@meta-comm.com... > "Bryan Parkoff" <no****@nospam. com> writes: > >> I have noticed that C programmers put static keyword beside >> global >> variable and global functions in C source codes. I believe that it >> is >> not >> necessary and it is not the practice in C++. Static keyword is >> useful >> inside struct, class, and function only unless you want to force >> local >> variable to be global variable so static is used. >> Do you have idea why most programmers do this? > > To enforce internal linkage. The same thing can be achieved by putting > globals into unnamed namespace.
Mikhail,
I have read a book what it explained internal linkage. I am not sure I understand what internal linkage means. I always place global variables and global variables inside namespace in C++ unlike C. Is it true that I don't need to use static keyword beside global variables and global variables if they are always in global scope. Please explain what is the difference between internal linkage and external linkage. Thanks...
3.5.2 A name is said to have linkage when it might denote the same object, reference, function, type, template, namespace or value as a name introduced by a declaration in another scope: - When a name has external linkage, the entity it denotes can be referred to by names from scopes of other translation units or from other scopes of the same translation unit. - When a name has internal linkage, the entity it denotes can be referred to by names from other scopes in the same translation unit. - When a name has no linkage, the entity it denotes cannot be referred to by names from other scopes.
Mikhail,
Thank you for explaining the difference between internal linkage and external linkage. I still don't understand your comment. I think that external linkage means to call variables or functions from first file to another second file. Internal linkage means to bind all local variables and local functions inside struct, class, or namespace. Also, another meaning is to hold global variables and global functions inside one file which they are inaccessible to other files.
For Example:
// First_File.h #ifndef FIRST_FILE.H #define FIRST_FILE.H
extern int Obj1; extern int Obj2; extern int Obj3;
External linkage, extern modifier.
extern void Read_Obj(void);
The same.
#endif
// First_File.cpp #include "First_File .h" #include <stdio.h>
int Obj1 = 0; int Obj2 = 0; int Obj3 = 0;
External linkage by default.
void Read_Obj(void) { for (int a = 0; a < 10; ++a) { Obj1 += 1; Obj2 += 2; Obj3 += 3; }
printf("Obj1: %d\nObj2: %d\nObj3: %d\n", Obj1, Obj2, Obj3); }
// Second_File.cpp #include "First_File .h"
int main(void) { Read_Obj();
return 0; }
My example source code above is external linkage. Please give me your example source which it is the internal linkage including static. I always add namespace each header files and source code files to prevent overwritten existing global variables according to my choice, but I can use struct or class if I want on depend of my design.
// internal linkage
static int obj1 = 0; // way 1: use static modifier int const obj2 = 0; // way 2: use const without extern modifier
static void foo();
namespace {
int obj3 = 0; // way 3: use unnamed namespace
void foo2();
}
The problem is that Read_Obj() function can't be forced inline. It is not the C++ Compiler's decision to grant or deny __forceinline because Read_Obj() function has external linkage. It creates two object files such as *.obj.
The __forceinline keyword is MSVC specific compiler extension. It has the same semantics as inline has, but while the latter is just a hint to the compiler, the former forces the compiler to do inlining (under some conditions).
IHMO, in this example the function Read_Obj is not a good candidate for inlining anyway.
It is impossible to copy Read_Obj() function from first *.obj to second *.obj which it pastes into main() function.
And what for? Those object files may end up in different programs/libraries. Don't forget about ODR (One Definition Rule).
If Read_Obj() function is placed in Second_File.cpp rather than First_File.cpp, __forceinline will work while C++ Compiler can always grant or deny __forceinline because Read_Obj() function has internal linkage.
The compiler does compile translation units one at a time, and it doesn't resolve any references between translation units, but a linker does. And it's too late for the linker to any inlining since all code have already been generated.
And as mentioned above, __forceinline does force inlining without any cost/performance analysis, but it makes sense for functions with internal linkage only.
Mikhail,
Thank you very much for explaining. I now understand clear. Do you
think that internal linkage with inline / without inline functions can gain
better performance than external linkage because it reduces too many needed
functions. Place static on global variables and global functions help to
hide from the public so they are always inaccessible like class' private
keyword.
I would use static keyword to place global variables and global
functions inside namespace rather than global scope so my large project
always looks neat with no mess.
Thanks again...
Bryan Parkoff
Mikhail Polatov wrote:
<snip> To enforce internal linkage. The same thing can be achieved by putting globals into unnamed namespace.
<snip>
IIRC, anonymous namespace objects have external linkage,
just like with any other namespace, including the global
one.
namespace {
extern int e_count; // explicit external
int count; // implicit external
static int s_count; // internal
extern const int ec_count; // external
const int c_count; // implicit internal
static const int sc_count; // explicit internal
}
I fell into the same trap when I learned that the standard
deprecates file-statics, and that one should use
anonymous namespaces instead. Until a team member
helpfully pointed out to me that the number of external
symbols had grown considerably since I started removing
deprecated stuff from the project :)
Marc
Marc Mutz <ma**@klaralvda lens-datakonsult.se> writes: Mikhail Polatov wrote: <snip> To enforce internal linkage. The same thing can be achieved by putting globals into unnamed namespace. <snip>
IIRC, anonymous namespace objects have external linkage, just like with any other namespace, including the global one.
namespace { extern int e_count; // explicit external int count; // implicit external static int s_count; // internal
extern const int ec_count; // external const int c_count; // implicit internal static const int sc_count; // explicit internal }
I fell into the same trap when I learned that the standard deprecates file-statics, and that one should use anonymous namespaces instead. Until a team member helpfully pointed out to me that the number of external symbols had grown considerably since I started removing deprecated stuff from the project :)
According to STD 3.5.4:
A name having namespace scope has external linkage if it is the name
of:
<snip>
- a namespace (7.3), unless it is declared within an unnamed namespace
But extern modifier overrides any implicit internal linkage.
--
Mikhail Polatov
Mikhail Polatov wrote:
<snip> According to STD 3.5.4: A name having namespace scope has external linkage if it is the name of: <snip> - a namespace (7.3), unless it is declared within an unnamed namespace
But extern modifier overrides any implicit internal linkage.
And your point is...?
3 A name having namespace scope (_basic.scope.n amespace_) has internal linkage if it is the name of
--an object, reference, function or function template that is explicitly declared static or,
Ie. in
namespace { static int i; static void f(); }
both 'i' and 'f' have internal linkage.
--an object or reference that is explicitly declared const and neither explicitly declared extern nor previously declared to have external linkage; or
Ie. in
namespace { const int i; }
'i' has internal linkage
--the name of a data member of an anonymous union.
4 A name having namespace scope has external linkage if it is the name of
--an object or reference, unless it has internal linkage; or
Ie. in
namespace { int i; }
'i' has external linkage
--a function, unless it has internal linkage; or
Ie. in
namespace { void f(); }
'f' has external linkage
--a named class (_class_), or an unnamed class defined in a typedef declaration in which the class has the typedef name for linkage purposes (_dcl.typedef_) ; or
<snip> --a namespace (_basic.namespa ce_), unless it is declared within an unnamed namespace.
Isn't that exactly what I summarised in my previous post?
Marc This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alexander Mahr |
last post by:
Dear Newsgroup,
I'm somehow confused with the usage of the static keyword.
I can see two function of the keyword static in conjunction with a data
member of a class.
1. The data member reffers in all objects of this class to the same data
Or in other word by using the static keyword all objects of one class
can share data. (This is...
|
by: katekukku |
last post by:
HI,
Could anyone please tell me what are static variables and what exactly
are there features.
I am a little bit confused.
Thank You
|
by: Bas Wassink |
last post by:
Hello there,
I'm having trouble understanding a warning produced by 'splint', a
code-checker. The warning produced is:
keywords.c: (in function keyw_get_string)
keywords.c:60:31: Released storage Keywords.Keyword reachable from global
A global variable does not satisfy its annotations when control
is transferred. (Use -globstate to...
|
by: Danny Din |
last post by:
Hi,
I have the following code –
public class Class1
{
public Class1()
|
by: The8thSense |
last post by:
how to declare static varible and static method inside a class ?
i try
"public static ABC as integer = 10"
and it said my declaration is invalid
Thanks
| |
by: Asfand Yar Qazi |
last post by:
For years, I've been putting everything that won't be needed outside a
compilation unit in anonymous namespaces, even editing my old files that did
things the 'old' way (using static linkage). Then suddenly I find this in the
KDE developer faq:
Q: 2.24. I put some functions in anonymous namespace and someone reverted it
and made those...
|
by: wizwx |
last post by:
There are two typical implementations of a singleton. The first one
is
to use a static pointer
class Singleton {
static Singleton * pSingleton;
public:
static Singleton * instance() {
if(pSingleton==NULL)
pSingleton = new Singleton;
return pSingleton;
|
by: Jess |
last post by:
Hello,
I learned that there are five kinds of static objects, namely
1. global objects
2. object defined in namespace scope
3. object declared static instead classes
4. objects declared static inside functions (i.e. local static
objects)
5. objects declared at file scope.
|
by: sergey.lukoshkin |
last post by:
Hello everyone!
My task is in converting numbers from string to int variables. I used
istringstream to perform it.
So I wrote simple test function. But it doesn't work as I expected
because val is not being changed while the program is running. Could
you please tell me why ??
#include <iostream>
#include <sstream>
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |