Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to avoid global variables and extern.

Reply
Thread Tools

How to avoid global variables and extern.

 
 
StephQ
Guest
Posts: n/a
 
      04-26-2007
I'm facing the following problem: I'm using the Gnu Scientific Library
(it is a C math library), and in particular random number generation
algorithms. Example code is:

....
gsl_rng * r;
....
double u = gsl_rng_uniform (r);

The problem is that r "represent" the iteration of the random number
generator algorithm, so it needs to be accessible every time you need
to generate a new random number. In C you can solve the problem by:

1) passing r to every function that needs to generate random numbers
(a burden....)
2) define r as global variable and use extern in evey file (not very
elegant)

I'm wondering if there is a better way to solve the problem in c++. I
also would like to toggle any explicit dependence in my code to the
gsl. This prompted the creation of a wrapper class like:

class RandomNumberGenerator
{
private:
gsl_rng* r;

public:

RandomNumberGenerator() ;

double generateUniform();

};

Then I would like to have something like a unique object of this class
available everywhere that does not need to be initilized (static?)
Is it possible to accomplish this task?
Do you have some better solutions to propose?
Thank you in advance for any help.

StephQ

 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      04-26-2007

"StephQ" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> I'm facing the following problem: I'm using the Gnu Scientific Library
> (it is a C math library), and in particular random number generation
> algorithms. Example code is:
>
> ...
> gsl_rng * r;
> ...
> double u = gsl_rng_uniform (r);
>
> The problem is that r "represent" the iteration of the random number
> generator algorithm, so it needs to be accessible every time you need
> to generate a new random number. In C you can solve the problem by:
>
> 1) passing r to every function that needs to generate random numbers
> (a burden....)
> 2) define r as global variable and use extern in evey file (not very
> elegant)
>


How about defining r as a global variable, then declaring it as extern in
just *one* header file, and simply including that header in any
implementation file which needs to use r?

(Bad name, "r", though. Something more descriptive of its purpose, and less
likely to have a name clash with local variables, would be better.)

-Howard


 
Reply With Quote
 
 
 
 
Roland Pibinger
Guest
Posts: n/a
 
      04-26-2007
On Thu, 26 Apr 2007 18:11:54 GMT, "Howard" wrote:
>"StephQ" wrote:
>> gsl_rng * r;
>> ...
>> double u = gsl_rng_uniform (r);
>>
>> The problem is that r "represent" the iteration of the random number
>> generator algorithm, so it needs to be accessible every time you need
>> to generate a new random number. In C you can solve the problem by:
>> 1) passing r to every function that needs to generate random numbers
>> (a burden....)
>> 2) define r as global variable and use extern in evey file (not very
>> elegant)

>
>How about defining r as a global variable, then declaring it as extern in
>just *one* header file, and simply including that header in any
>implementation file which needs to use r?


Non-const global variables have severe drawbacks and are usually not
considered good style. Since the variable is needed to '"represent"
the iteration of the random number generator algorithm' I don't think
it's a 'burden' to pass it to functions.


--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      04-26-2007
Roland Pibinger wrote:
> On Thu, 26 Apr 2007 18:11:54 GMT, "Howard" wrote:
>> "StephQ" wrote:
>>> gsl_rng * r;
>>> ...
>>> double u = gsl_rng_uniform (r);
>>>
>>> The problem is that r "represent" the iteration of the random number
>>> generator algorithm, so it needs to be accessible every time you need
>>> to generate a new random number. In C you can solve the problem by:
>>> 1) passing r to every function that needs to generate random numbers
>>> (a burden....)
>>> 2) define r as global variable and use extern in evey file (not very
>>> elegant)

>> How about defining r as a global variable, then declaring it as extern in
>> just *one* header file, and simply including that header in any
>> implementation file which needs to use r?

>
> Non-const global variables have severe drawbacks and are usually not
> considered good style. Since the variable is needed to '"represent"
> the iteration of the random number generator algorithm' I don't think
> it's a 'burden' to pass it to functions.



I second that.

For classes at the "application" level, I usually practice passing
around an "environment" class that contains resources such as a
preferences manager, logging manager and anything else like this.

In the Austria C++ alpha (http://netcabletv.org/public_releases/) there
is an Environment (at_env.h) template which does just that.
 
Reply With Quote
 
Greg Herlihy
Guest
Posts: n/a
 
      04-27-2007



On 4/26/07 11:03 AM, in article
(E-Mail Removed). com, "StephQ"
<(E-Mail Removed)> wrote:

> I'm facing the following problem: I'm using the Gnu Scientific Library
> (it is a C math library), and in particular random number generation
> algorithms. Example code is:
>
> ...
>
> gsl_rng * r;
> ...
>
> double u = gsl_rng_uniform (r);
>
> The problem is that r "represent" the iteration of the random number
> generator algorithm, so it needs to be accessible every time you need
> to generate a new random number. In C you can solve the problem by:
>
> 1) passing r to every function that needs to generate random numbers
> (a burden....)
> 2) define r as global variable and use extern in evey file (not very
> elegant)


Why not implement a function that wraps the library routine:

double GenerateRandomNumber()
{
static gsl_rng * r = NULL;

return gsl_rng_uniform( r);
}

Greg

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      04-27-2007
"StephQ" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> I'm facing the following problem: I'm using the Gnu Scientific Library
> (it is a C math library), and in particular random number generation
> algorithms. Example code is:
>
> ...
> gsl_rng * r;
> ...
> double u = gsl_rng_uniform (r);
>
> The problem is that r "represent" the iteration of the random number
> generator algorithm, so it needs to be accessible every time you need
> to generate a new random number. In C you can solve the problem by:
>
> 1) passing r to every function that needs to generate random numbers
> (a burden....)
> 2) define r as global variable and use extern in evey file (not very
> elegant)
>
> I'm wondering if there is a better way to solve the problem in c++. I
> also would like to toggle any explicit dependence in my code to the
> gsl. This prompted the creation of a wrapper class like:
>
> class RandomNumberGenerator
> {
> private:
> gsl_rng* r;
>
> public:
>
> RandomNumberGenerator() ;
>
> double generateUniform();
>
> };
>
> Then I would like to have something like a unique object of this class
> available everywhere that does not need to be initilized (static?)
> Is it possible to accomplish this task?
> Do you have some better solutions to propose?
> Thank you in advance for any help.


class RandomNumberGenerator
{
public:
double GenerateUniform() { return gsl_rng_uniform (&Seed) };
private:
static gsl_rng Seed;
};

gsl_rng RandomNumberGenerator::Seed = 0;

Notice, Seed is declared as an instance, not as a pointer. gsl_rng_uniform
needs the address of the variable which is accomplished by passing the
address of seed. In your sample you never allocated storage for r.


 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      04-27-2007
"Greg Herlihy" <(E-Mail Removed)> wrote in message
news:C256AEF3.83FE%(E-Mail Removed)...
>
>
>
> On 4/26/07 11:03 AM, in article
> (E-Mail Removed). com, "StephQ"
> <(E-Mail Removed)> wrote:
>
>> I'm facing the following problem: I'm using the Gnu Scientific Library
>> (it is a C math library), and in particular random number generation
>> algorithms. Example code is:
>>
>> ...
>>
>> gsl_rng * r;
>> ...
>>
>> double u = gsl_rng_uniform (r);
>>
>> The problem is that r "represent" the iteration of the random number
>> generator algorithm, so it needs to be accessible every time you need
>> to generate a new random number. In C you can solve the problem by:
>>
>> 1) passing r to every function that needs to generate random numbers
>> (a burden....)
>> 2) define r as global variable and use extern in evey file (not very
>> elegant)

>
> Why not implement a function that wraps the library routine:
>
> double GenerateRandomNumber()
> {
> static gsl_rng * r = NULL;
>
> return gsl_rng_uniform( r);
> }


Yes, this would work better than a class, IMO. Just have a static variable
in a function. Of course it should probably be:

double GenerateRandomNumber()
{
static gsl_rng r = 0;
return gsl_rng_uniform(&r);
}

or whatever you initialize gsl_rng with.


 
Reply With Quote
 
anon
Guest
Posts: n/a
 
      04-27-2007
Jim Langston wrote:
> "StephQ" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>> I'm facing the following problem: I'm using the Gnu Scientific Library
>> (it is a C math library), and in particular random number generation
>> algorithms. Example code is:
>>
>> ...
>> gsl_rng * r;
>> ...
>> double u = gsl_rng_uniform (r);
>>
>> The problem is that r "represent" the iteration of the random number
>> generator algorithm, so it needs to be accessible every time you need
>> to generate a new random number. In C you can solve the problem by:
>>
>> 1) passing r to every function that needs to generate random numbers
>> (a burden....)
>> 2) define r as global variable and use extern in evey file (not very
>> elegant)
>>
>> I'm wondering if there is a better way to solve the problem in c++. I
>> also would like to toggle any explicit dependence in my code to the
>> gsl. This prompted the creation of a wrapper class like:
>>
>> class RandomNumberGenerator
>> {
>> private:
>> gsl_rng* r;
>>
>> public:
>>
>> RandomNumberGenerator() ;
>>
>> double generateUniform();
>>
>> };
>>
>> Then I would like to have something like a unique object of this class
>> available everywhere that does not need to be initilized (static?)
>> Is it possible to accomplish this task?
>> Do you have some better solutions to propose?
>> Thank you in advance for any help.

>
> class RandomNumberGenerator
> {
> public:
> double GenerateUniform() { return gsl_rng_uniform (&Seed) };


Would it be better to make this static as well? Then you wouldn't have
to create objects of this class.

> private:
> static gsl_rng Seed;
> };
>
> gsl_rng RandomNumberGenerator::Seed = 0;
>
> Notice, Seed is declared as an instance, not as a pointer. gsl_rng_uniform
> needs the address of the variable which is accomplished by passing the
> address of seed. In your sample you never allocated storage for r.
>
>

 
Reply With Quote
 
StephQ
Guest
Posts: n/a
 
      04-27-2007
> > class RandomNumberGenerator
> > {
> > public:
> > double GenerateUniform() { return gsl_rng_uniform (&Seed) };

>
> Would it be better to make this static as well? Then you wouldn't have
> to create objects of this class.
>
> > private:
> > static gsl_rng Seed;
> > };

>
> > gsl_rng RandomNumberGenerator::Seed = 0;


Do you see any evident problem in the the following solution?

class RandomNumber
{
private:
const gsl_rng_type* T;
gsl_rng* r;

public:

RandomNumber() ;

// Uniform random number generator.
double generateUniform();

// Distributions random number generators.
double generateGaussian(double mu, double sigma);

......

};

static RandomNumber randomNumber;

I would like to avoid passing r around because:
1) it's related to the use of the gsl library. And I would like to be
felixible about the library used for random number generation.
2) in stochastic simulations too many algorithms depend on the
generations of random numbers. I would really have to pass r even to
class constructors and so on.....

p.s. I know it is not a good name, r. It was just an example from the
gsl documentation

Thank you
StephQ

 
Reply With Quote
 
anon
Guest
Posts: n/a
 
      04-27-2007
StephQ wrote:
>>> class RandomNumberGenerator
>>> {
>>> public:
>>> double GenerateUniform() { return gsl_rng_uniform (&Seed) };

>> Would it be better to make this static as well? Then you wouldn't have
>> to create objects of this class.
>>
>>> private:
>>> static gsl_rng Seed;
>>> };
>>> gsl_rng RandomNumberGenerator::Seed = 0;

>
> Do you see any evident problem in the the following solution?
>
> class RandomNumber
> {
> private:
> const gsl_rng_type* T;
> gsl_rng* r;
>
> public:
>
> RandomNumber() ;
>
> // Uniform random number generator.
> double generateUniform();
>
> // Distributions random number generators.
> double generateGaussian(double mu, double sigma);
>
> ......
>
> };
>
> static RandomNumber randomNumber;
>
> I would like to avoid passing r around because:
> 1) it's related to the use of the gsl library. And I would like to be
> felixible about the library used for random number generation.
> 2) in stochastic simulations too many algorithms depend on the
> generations of random numbers. I would really have to pass r even to
> class constructors and so on.....


So, you want to use the solution from Greg Herlihy:
double GenerateRandomNumber()
{
static gsl_rng * r = NULL;

return gsl_rng_uniform( r);
}

Or you can put this function to a class, and declare it static, you will
not have to pass your r to constructor:
/// in the header file
class RandomNumber
{
public:
static double generate();
}

/// in the cpp file
double RandomNumber::generate()
{
static gsl_rng * r = NULL;
return gsl_rng_uniform( r );
}

You can call it like:
RandomNumber::generate()
from any file including RandomNumber header file
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
Avoid having a SQL express for web parts and avoid personalization Roger23 ASP .Net 2 10-12-2006 10:54 PM
Avoid wasting time or how to avoid initialization Alexander Malkis C++ 8 04-13-2004 11:23 PM



Advertisments