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

Basic/simple question regarding header file

If in one of the header file, a constant is defined:

const UINT UM_CAL_DONE = func();

and it is included in several other .cpp file. Does the func()
excecuted many times? If it is included in multithread application.
data in the func() must be synchonized?

Feb 3 '06 #1
5 1528
My goal is to use func() to generate a constant which can be used by
many threads or functions.

Feb 3 '06 #2
kathy wrote:
If in one of the header file, a constant is defined:

const UINT UM_CAL_DONE = func();

and it is included in several other .cpp file. Does the func()
excecuted many times?
Yes, very likely. To prevent that, declare your constant 'extern'
and only define (and initialise) it in a _single_ translation unit.
If it is included in multithread application.
data in the func() must be synchonized?


That's beyond the scope of this newsgroup, but probably yes.

V
--
Please remove capital As from my address when replying by mail
Feb 3 '06 #3
Victor Bazarov wrote:
kathy wrote:
If in one of the header file, a constant is defined:

const UINT UM_CAL_DONE = func();

and it is included in several other .cpp file. Does the func()
excecuted many times?


Yes, very likely. To prevent that, declare your constant 'extern'
and only define (and initialise) it in a _single_ translation unit.


Just to prove Victors answer compile the following program with
g++-3.3 -g -Wall -c -o try.o try.cc
g++-3.3 -g -Wall -c -o foo.o foo.cc
g++-3.3 -g -Wall -o try try.o foo.o

try.h
=====
#ifndef TRY_H
#define TRY_H

int mkCint();
const int cInt = mkCint();

#endif

try.cc
======
#include "try.h"
#include <iostream>

int main()
{
std::cout << cInt << std::endl;
}

int mkCint()
{
std::cout << "mkCint()" << std::endl;
return 2;
}

foo.cc
======
#include "try.h"

The output is
UNIX> ./try
mkCint()
mkCint()
2

Note: the program segfaults if compiled with g++-3.4/g++-4.0 (I've just
submitted a bug report). As work-around write to stdout in mkCint().

Regards, Stephan

Feb 3 '06 #4
>> > If in one of the header file, a constant is defined:
>
> const UINT UM_CAL_DONE = func();
>
> and it is included in several other .cpp file. Does the func()
> excecuted many times?
Yes, very likely. To prevent that, declare your constant 'extern'
and only define (and initialise) it in a _single_ translation unit.


Just to prove Victors answer compile the following program with
g++-3.3 -g -Wall -c -o try.o try.cc
g++-3.3 -g -Wall -c -o foo.o foo.cc
g++-3.3 -g -Wall -o try try.o foo.o

try.h
=====
#ifndef TRY_H
#define TRY_H

int mkCint();
const int cInt = mkCint();

#endif

try.cc
======
#include "try.h"
#include <iostream>

int main()
{
std::cout << cInt << std::endl;
}

int mkCint()
{
std::cout << "mkCint()" << std::endl;
return 2;
}

foo.cc
======
#include "try.h"

The output is
UNIX> ./try
mkCint()
mkCint()
2


Just to provide Stephans example following Victors answer:

try.h
=====

#ifndef TRY_H
#define TRY_H

int mkCint();
extern const int cInt;

#endif
try.cc
======

#include "try.h"
#include <iostream>

const int cInt = mkCint();

int main()
{
std::cout << cInt << std::endl;
}

int mkCint()
{
std::cout << "mkCint()" << std::endl;
return 2;
}
foo.cc
======

#include "try.h"
The output is
=============

$ ./try
mkCint()
2
Note: the program segfaults if compiled with g++-3.4/g++-4.0 (I've just
submitted a bug report). As work-around write to stdout in mkCint().


Approved for

$ g++ --version
g++ (GCC) 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu9)

Feb 4 '06 #5
thank you
"Marco Wahl <0@0>" <ma********@gmail.com>
??????:11*********************@o13g2000cwo.googleg roups.com...
> If in one of the header file, a constant is defined:
>
> const UINT UM_CAL_DONE = func();
>
> and it is included in several other .cpp file. Does the func()
> excecuted many times?

Yes, very likely. To prevent that, declare your constant 'extern'
and only define (and initialise) it in a _single_ translation unit.


Just to prove Victors answer compile the following program with
g++-3.3 -g -Wall -c -o try.o try.cc
g++-3.3 -g -Wall -c -o foo.o foo.cc
g++-3.3 -g -Wall -o try try.o foo.o

try.h
=====
#ifndef TRY_H
#define TRY_H

int mkCint();
const int cInt = mkCint();

#endif

try.cc
======
#include "try.h"
#include <iostream>

int main()
{
std::cout << cInt << std::endl;
}

int mkCint()
{
std::cout << "mkCint()" << std::endl;
return 2;
}

foo.cc
======
#include "try.h"

The output is
UNIX> ./try
mkCint()
mkCint()
2


Just to provide Stephans example following Victors answer:

try.h
=====

#ifndef TRY_H
#define TRY_H

int mkCint();
extern const int cInt;

#endif
try.cc
======

#include "try.h"
#include <iostream>

const int cInt = mkCint();

int main()
{
std::cout << cInt << std::endl;
}

int mkCint()
{
std::cout << "mkCint()" << std::endl;
return 2;
}
foo.cc
======

#include "try.h"
The output is
=============

$ ./try
mkCint()
2
Note: the program segfaults if compiled with g++-3.4/g++-4.0 (I've just
submitted a bug report). As work-around write to stdout in mkCint().


Approved for

$ g++ --version
g++ (GCC) 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu9)

Feb 4 '06 #6

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

Similar topics

7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
8
by: Paminu | last post by:
I am trying to split my program in different parts. I have a file called mainfile.c that contains the main() function, some global variables and a few other functions. I then have a file...
13
by: Pete | last post by:
I'm cross posting from mscom.webservices.general as I have received no answer there: There has been a number of recent posts requesting how to satisfactorily enable BASIC authorization at the...
1
by: Scott Haner via DotNetMonster.com | last post by:
Hey; I've done a decent amount of VB.net in University, but the next stage for a major project we have been asked to use 'modulization' and that the judges want to see as much code and functions...
4
by: joe | last post by:
I have the following header file generated by Java JNI's header file generator (see bottom). Obviously I will write a source file which will provide an implementation of these methods. I have...
4
by: gaurav.dube | last post by:
I am facing a problem with warning removal I have got a header file it contains extern declarations of lot of variables (say 100) This header file is included in hundreds of .c files. Some of...
3
by: sam_cit | last post by:
Hi Everyone, I have seen in some project where functions are declared as extern, what is the possible reason to do this? To my best understanding, if some other file wan't to invoke this...
4
by: noone | last post by:
Hi. I've got a fella working for me who is trying to convince me to change our coding standards to using separate .h and .cc files for definitions and implementations. I know this is a...
9
by: Peskov Dmitry | last post by:
It is a very basic question.Surely i got something wrong in my basic understanding. //Contents of file1.cpp using namespace std; #include <iostream> template <typename T> class my_stack;
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...

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.