473,856 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange problem about static data

I am having a strange problem. I have a static int with an initial
value in a header file which I include from two source files. In one
file, I assign a value to that int but the other file don't get it. I
have printed the value from both files. The first line printed from the
first file shows the assigned value but the second line printed from
the second file shows the initial value. How can this happen?

Here is an example:

File: Header.h
==============

#ifndef _HEADER_H_
#define _HEADER_H_
static int g_iVerboseLevel = 6;
#endif

File: Header2.h
===============
#ifndef _HEADER2_H_
#define _HEADER2_H_

void ChangeValue();

#endif

File: File2.cpp
===============
#include <iostream>
#include "Header.h"

using namespace std;
void ChangeValue()
{
std::cout << "Value from ChangeValue is " << g_iVerboseLevel <<
'\n';
}

File: File1.cpp
===============
#include "Header.h"
#include <iostream>
#include "Header2.h"

using namespace std;

int main(void)
{
g_iVerboseLevel = 4;
std::cout << "Value of g_iVerboseLevel from main is " <<
g_iVerboseLevel << '\n';
ChangeValue();
std::cout << "Value of g_iVerboseLevel from main is " <<
g_iVerboseLevel << endl;
return 0;
}

The output:
Value of g_iVerboseLevel from main is 4
Value from ChangeValue is 6
Value of g_iVerboseLevel from main is 4

Can anybody explain?

Jul 23 '05 #1
5 1285


Russoue wrote:
I am having a strange problem. I have a static int with an initial
value in a header file which I include from two source files. In one
file, I assign a value to that int but the other file don't get it. I
have printed the value from both files. The first line printed from the
first file shows the assigned value but the second line printed from
the second file shows the initial value. How can this happen?

Here is an example:

File: Header.h
==============

#ifndef _HEADER_H_
#define _HEADER_H_
static int g_iVerboseLevel = 6;
#endif

File: Header2.h
===============
#ifndef _HEADER2_H_
#define _HEADER2_H_

void ChangeValue();

#endif

File: File2.cpp
===============
#include <iostream>
#include "Header.h" After you include this file.
There is g_iVerboseLevel which is can be seen only this file due to
keyword 'static' and this var has initialised as 6.
So, File2.o will have this var inside.

using namespace std;
void ChangeValue()
{
std::cout << "Value from ChangeValue is " << g_iVerboseLevel <<
'\n';
You've print out g_iVerboseLevel which is inside File2.o (val = 6).
}

File: File1.cpp
===============
#include "Header.h"
You've included this header file.
So, you have g_iVerboseLevel for File1.o, and can be seen only this
file.
#include <iostream>
#include "Header2.h" include ChangeValue(); for use below.
using namespace std;

int main(void)
{
g_iVerboseLevel = 4; Assign g_iVerboseLevel (inside File1.o)
std::cout << "Value of g_iVerboseLevel from main is " <<
g_iVerboseLevel << '\n'; Print out g_iVerboseLevel (inside File1.o which is 4)
ChangeValue(); Print out g_iVerboseLevel (inside File2.o which is 6)
std::cout << "Value of g_iVerboseLevel from main is " <<
g_iVerboseLevel << endl; Print out g_iVerboseLevel (inside File1.o again). return 0;
}

The output:
Value of g_iVerboseLevel from main is 4
Value from ChangeValue is 6
Value of g_iVerboseLevel from main is 4

Can anybody explain?


conclusion:
You have 2 g_iVerboseLevel in your executable program.
Regards,

Jul 23 '05 #2


Russoue wrote:
I am having a strange problem. I have a static int with an initial
value in a header file which I include from two source files. In one
file, I assign a value to that int but the other file don't get it. I
have printed the value from both files. The first line printed from the
first file shows the assigned value but the second line printed from
the second file shows the initial value. How can this happen?

Here is an example:

File: Header.h
==============

#ifndef _HEADER_H_
#define _HEADER_H_
static int g_iVerboseLevel = 6;
#endif

File: Header2.h
===============
#ifndef _HEADER2_H_
#define _HEADER2_H_

void ChangeValue();

#endif

File: File2.cpp
===============
#include <iostream>
#include "Header.h" After you include this file.
There is g_iVerboseLevel which is can be seen only this file due to
keyword 'static' and this var has initialised as 6.
So, File2.o will have this var inside.

using namespace std;
void ChangeValue()
{
std::cout << "Value from ChangeValue is " << g_iVerboseLevel <<
'\n';
You've print out g_iVerboseLevel which is inside File2.o (val = 6).
}

File: File1.cpp
===============
#include "Header.h"
You've included this header file.
So, you have g_iVerboseLevel for File1.o, and can be seen only this
file.
#include <iostream>
#include "Header2.h" include ChangeValue(); for use below.
using namespace std;

int main(void)
{
g_iVerboseLevel = 4; Assign g_iVerboseLevel (inside File1.o)
std::cout << "Value of g_iVerboseLevel from main is " <<
g_iVerboseLevel << '\n'; Print out g_iVerboseLevel (inside File1.o which is 4)
ChangeValue(); Print out g_iVerboseLevel (inside File2.o which is 6)
std::cout << "Value of g_iVerboseLevel from main is " <<
g_iVerboseLevel << endl; Print out g_iVerboseLevel (inside File1.o again). return 0;
}

The output:
Value of g_iVerboseLevel from main is 4
Value from ChangeValue is 6
Value of g_iVerboseLevel from main is 4

Can anybody explain?


conclusion:
You have 2 g_iVerboseLevel in your executable program.
Regards,

Jul 23 '05 #3
Thanks a lot Prawit Chaivong. I understood the problem. Your
explanation was fine and easy to understand. Should I move that
variable to a cpp file? Can you suggest me a solution?

Jul 23 '05 #4
Thanks Prawit. I solved the problem by declaring the int (without
making it static) in a cpp file and including it from other cpp files
using the "extern" keywordl.

Jul 23 '05 #5
Russoue wrote:
[problem redacted, Prawit solved it for you]
Here is an example:

File: Header.h
==============

#ifndef _HEADER_H_
#define _HEADER_H_
static int g_iVerboseLevel = 6;
#endif


Do not define your own identifiers with a leading underscore followed
by an uppercase letter. Such identifiers are reserved to the
implementation.

Instead, use:

#ifndef HEADER_H_
#define HEADER_H_
// etc...
#endif

as your include guard.
Jul 23 '05 #6

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

Similar topics

0
1732
by: john | last post by:
Hi,All Gurus: It is kind of complicated, please bear with me and let me know if you have any questions. Thanks a lot in advance. John I have a csharp method, using emit to dynamically generate classes & method depends on the meta table in the database,here is my problem 1) One method I generated works when it was called, but another set of method does not work, it generate the following error
3
1768
by: Alfonso Morra | last post by:
I have some code that I am porting over from C. It is full of static functions and global variables. I have got around this by wrapping most of the code in a singleton object. However, I am findng that in my method definitions, I am having to fully scope the names of any other functions I am calling. I do not understand this. The clas declaration code look some thing like this: class A: public Singleton<A> { public:
2
1791
by: TB | last post by:
I am seeing a very strange problem as follows... I have a loop where a fair amount of processing is going on and near the top of the loop I access a class that has only static helper functions to perform some calculations. After some number of iterations, randomly, I'll get an uncaught NullValueException error on one of these calls, as if the class name is being treated as an object reference and is null. Here is some psuedo-code to...
0
1085
by: sqcliu | last post by:
I encounter a strange problem using platform invoke using C#. The senario is this: I have a dll which uses a 3rd party static link library (lib). Inside the lib, there are some C structures defined by not exposed to user, in the dll, that data type can be treated as an opaque type. I use C# and unamanaged C++ to write two programs to use the dll. The C++ version works perfectly, but the C# version always get some exception (Object...
0
2273
by: Diego | last post by:
Hello. I need help. I'm writing an application that requires audio recording and I have decided to use waveform audio interface. I'm able to process WIM_DATA message but when i call waveInStop the buffer is not marked as done until it's full. The documentation says that calling the sequence WaveinStop(HWAVEIN) we stop recording marking a buffer as done, end the member dwbyterecorded contains the amount of data. why calling WaveInstop...
9
2246
by: hfinster | last post by:
Hello, could somebody please shed light on the following problem with g++ (4.03 and 3.3.6 as well)? Obviously, the copy constructor is not executed, if I assign the result of a function call to a (new) variable. Instead the memory location (address) of a temporary result variable from the function is used. This is my test code:
16
2405
by: Victor | last post by:
I have a strange problem in my website. I configured my website to run under 2 worker processes. (web garden enabled). and I stored my user information in the current httpcontext(like Httpcontext.current.items.add("__currentuser", myUserobject") and retrieve the user object like (user = (UserType)Httpcontext.current.items). the website authentication based on the form authentication. and I used login control and customized the validation...
4
1316
by: Andi Wolf | last post by:
Hello, as i belived c# will always transfer the variables as by vale if not marked with a ref infront of the decleration. By using the folowing code there seams to be (at least for me) a strange behavior: namespace xyz { public class Class1
4
3126
by: Asylus | last post by:
Thank you in advance for any help you may be able to provide. I am trying to create a file using DataOutputStream. I'm using the following code to write the file: public static void rewriteFile(String artistAlbum) throws IOException { System.out.println("Writing file: " + dataB); DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(dataB)); FileOutputStream midman = new FileOutputStream(dataB);
0
9922
marktang
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9766
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11066
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10702
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10391
tracyyun
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9536
agi2029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5763
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4584
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
3
3203
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.