Strange problem about static data

R

Russoue

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?
 
P

Prawit Chaivong

Russoue said:
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,
 
P

Prawit Chaivong

Russoue said:
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,
 
R

Russoue

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?
 
R

Russoue

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.
 
R

red floyd

Russoue said:
[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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,206
Messages
2,571,069
Members
47,675
Latest member
KevinStepp

Latest Threads

Top