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

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 1264


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
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...
3
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...
2
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...
0
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...
0
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...
9
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...
16
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...
4
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...
4
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...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...
0
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...
0
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,...
0
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...
0
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,...

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.