sharing variables across mulitple files

A

awhan.iitk

I have a set of variables that I want to share across mulitple c++
files. I was using the extern method so far. Is there any other way to
do the same. The variables are not constants and I get the values
during run time.
 
J

Jim Langston

I have a set of variables that I want to share across mulitple c++
files. I was using the extern method so far. Is there any other way to
do the same. The variables are not constants and I get the values
during run time.

extern is the way you do it normally, other than passing the variables as
parameters.
 
A

awhan.iitk

extern is the way you do it normally, other than passing the variables as
parameters.

thanks for the reply. yes i should have also mentioned that i did not
want to pass the variables all the time. the reason i am looking for
alternate ways is after learning that memory for global variables is
reserved in the heap and a lot of global variables can create problems
for a program.
 
J

Jim Langston

Pete said:
Not usually. They go in the same memory area as static data.


Well, as far as memory usage, maybe. But more important is that they
introduce sneak paths for data modification, which can make program
analysis and maintenance harder. Sometimes sharing is unavoidable, of
course.

I have one project where I am forces to use global variables because of the
engine I am using having multiple callbbacks and it's impossible for me to
pass the data. One thing I did was I put all the global variables in a
structure and have just one instance of the structure. I'm not sure if it
simplifies anything, but it helps.
 
C

Cynic

I have one project where I am forces to use global variables because of the
engine I am using having multiple callbbacks and it's impossible for me to
pass the data. One thing I did was I put all the global variables in a
structure and have just one instance of the structure. I'm not sure if it
simplifies anything, but it helps.

the structure idea is actually novel. how about if u just include the
prog that has the variables defined in them, and call a function from
the new one to the old one?
will that work?
 
D

dizzy

Jim said:
I have one project where I am forces to use global variables because of
the engine I am using having multiple callbbacks and it's impossible for
me to
pass the data. One thing I did was I put all the global variables in a
structure and have just one instance of the structure. I'm not sure if it
simplifies anything, but it helps.

Sounds like a very bad C callback mechanism. It is a "C" callback mechanism
because otherwise it would have allowed generic functors and full compile
time type checking on the given functions and arguments. It is a "bad C
callback mechanism" because otherwise it would take in one form or another
an opaque argument (usually under the form of a "void*" argument) to
register along with the callback and that value is passed back to the
callback, the argument usually being used to transmit state from the code
registering the callback to the callback.

Yeah, organizing the data to be shared in a struct may help (it is usually
required with the "void*" passing method described above).
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top