sharing variables across mulitple files
  Home FAQ Contact Sign in
comp.lang.c++ only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.c++ Profile…
 Up
sharing variables across mulitple files         


Author: awhan.iitk
Date: May 6, 2008 02:58

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.
6 Comments
Re: sharing variables across mulitple files         


Author: Jim Langston
Date: May 6, 2008 03:07

awhan.iitk@gmail.com wrote:
> 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.

--
Jim Langston
tazmaster@rocketmail.com
no comments
Re: sharing variables across mulitple files         


Author: awhan.iitk
Date: May 6, 2008 03:26

On May 6, 3:07 pm, "Jim Langston" rocketmail.com> wrote:
>
> extern is the way you do it normally, other than passing the variables as
> parameters.
>
> --
> Jim Langston
> tazmas...@rocketmail.com

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.
no comments
Re: sharing variables across mulitple files         


Author: Pete Becker
Date: May 6, 2008 04:00

On 2008-05-06 06:26:32 -0400, "awhan.iitk@gmail.com"
gmail.com> said:
> On May 6, 3:07 pm, "Jim Langston" rocketmail.com> wrote:
>>
>> extern is the way you do it normally, other than passing the variables as
>> parameters.
>>
>> --
>> Jim Langston
>> tazmas...@rocketmail.com
>
> 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

Not usually. They go in the same memory area as static data.
> and a lot of global variables can create problems
> for a program.
Show full article (1.07Kb)
no comments
Re: sharing variables across mulitple files         


Author: Jim Langston
Date: May 6, 2008 05:50

Pete Becker wrote:
> On 2008-05-06 06:26:32 -0400, "awhan.iitk@gmail.com"
> gmail.com> said:
>
>> On May 6, 3:07 pm, "Jim Langston" rocketmail.com> wrote:
>>>
>>> extern is the way you do it normally, other than passing the
>>> variables as parameters.
>>>
>>> --
>>> Jim Langston
>>> tazmas...@rocketmail.com
>>
>> 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
>
> Not usually. They go in the same memory area as static data.
> ...
Show full article (1.33Kb)
no comments
Re: sharing variables across mulitple files         


Author: Cynic
Date: May 6, 2008 19:26

On May 6, 5:50 pm, "Jim Langston" rocketmail.com> wrote:
> Pete Becker wrote:
>> On 2008-05-06 06:26:32 -0400, "awhan.i...@gmail.com"
>> gmail.com> said:
>
>>> On May 6, 3:07 pm, "Jim Langston" rocketmail.com> wrote:
>
>>>> extern is the way you do it normally, other than passing the
>>>> variables as parameters.
>
>>>> --
>>>> Jim Langston
>>>> tazmas...@rocketmail.com
>
>>> 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
>
>> Not usually. They go in the same memory area as static data. ...
Show full article (1.63Kb)
no comments
Re: sharing variables across mulitple files         


Author: dizzy
Date: May 7, 2008 01:39

Jim Langston wrote:
> 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).

--
Dizzy
no comments