473,378 Members | 1,605 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,378 software developers and data experts.

"extern" for the same file leads to wrong result. bug or feature?

If "extern" is specified for a variable which is already in the same cpp file
then the compiler (VS6) creates a new instance of the var in each thread.
Is this a bug or a feature?
Which is standard conform?

Example:

test.cpp:
int gi = 0;

test.cpp (ie. same file)

thread_func(...)
{
extern int gi;
gi++;
//...
}

thread1 starts and exits. gi now 1 as expected.
thread2 starts and exits. gi now has value 1. Shouldn't it be 2 ?

If the "extern" declaration is not used then gi is 2.
Or, if the "extern" declaration is used and gi is moved to another cpp
then too is gi 2.

What's the correct (std conform) behaviour?
// U.Mutlu
Jul 23 '05 #1
1 1211
Uenal Mutlu wrote:
If "extern" is specified for a variable which is already in the same cpp file
then the compiler (VS6) creates a new instance of the var in each thread.
Threads are not defined by C++ _language_. You will find more information
in comp.programming.threads or in microsoft.public.vc.language.
Is this a bug or a feature?
Unknown.
Which is standard conform?
Nothing. Or both. Threads are not part of the Standard, so whatever
happens is fine by it.

Example:

test.cpp:
int gi = 0;
This declares an object 'gi' that (by default) has external linkage.
test.cpp (ie. same file)

thread_func(...)
{
extern int gi;
This is another declaration of 'gi'. This 'gi' is the same 'gi' as you
declared and defined outside of any function.
gi++;
//...
}

thread1 starts and exits. gi now 1 as expected.
thread2 starts and exits. gi now has value 1. Shouldn't it be 2 ?
Unknown. Threads are not defined by the language.
If the "extern" declaration is not used then gi is 2.
Or, if the "extern" declaration is used and gi is moved to another cpp
then too is gi 2.

What's the correct (std conform) behaviour?


Either. Both. Unknown. The use of threads introduces the conditions
that C++ Standard cannot account for.

V
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Shea Martin | last post by:
I am trying to use a system call which takes a function ptr. My compiler won't compile the code if I give the system_call a ptr to a class member function, A::func(). To combat this, I created an...
3
by: Andy | last post by:
Hi, This is just a simple hello world program. #include <iostream> using namespace std; int main() { cout<<"Hello,world\n"; }
2
by: flyaflya | last post by:
I want use a c lib in vc6. so I use extern "C" to c function, include three files "test.cpp", "b.h" and "b.c": //************************************************ // test.cpp #include...
12
by: G Patel | last post by:
I've seen some code with extern modifiers in front of variables declared inside blocks. Are these purely definitions (no definition) or are they definitions with static duration but external...
19
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
5
by: jchludzinski | last post by:
I have 3 files (see below: a.h, w.c, ww.c). I would like to use a single x (declared somewhere) which would global to both compilation units: w.c & ww.c. No matter where I place the "extern"...
4
by: mimi | last post by:
The programmer indicates to the compiler that a function is written in a different programming language using a linkage directives.It is intuitive that extern "SomeLanguage" is used to declare...
17
by: William Pursell | last post by:
I debated whether this belongs in comp.lang.c++, and finally decided that it belongs here, because it really is a C question... Suppose I have a perfectly friendly, harmless library, libfoo,...
19
by: Karel Van Laer | last post by:
Hi everyone, I need to be able to mix C and C++. The main program is written in C and needs to access C++ code. To be more specific it's a user defined function in fluent, but this should not...
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
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.