Simple question on static variables

A

Alfonso Morra

Hi,

I have a function declared thus:

int foo( int, int );

In the definition, I want to have a variable that is expensive to
create. I want to be able to save the state of this variable accross
calls - so I declare the var as a static - nothing new here.

My func defintion now looks something like this:

int foo(int a, int b) {
static Barney* = expensive_function()
....
}

My question are:

1). Is the expensive function called every, time my function is called
(I can hear a deafening YES to the question - but I'll still ask anyways)

2). Is there a way that I can have this expensive function called once
and ONLY once - the first time the function foo is called.

I thought of using bool flags, but its not clar what value they'll be
initialized at (true,false), the first time the function foo is called,
and if I asign an initialization value to a flag, the value will be
initialized to a specific pt, everytime the function is entered, the
flags will get reset - hardly the behaviour I want.

Look forward to an interesting solution. tkx
 
R

Robert Harris

Alfonso said:
Hi,

I have a function declared thus:

int foo( int, int );

In the definition, I want to have a variable that is expensive to
create. I want to be able to save the state of this variable accross
calls - so I declare the var as a static - nothing new here.

My func defintion now looks something like this:

int foo(int a, int b) {
static Barney* = expensive_function()
You can only initialise a static variable to a constant expression (see
6.7.8 of the standard) so it isn't legal.
You need something like:

int foo(int a, it b) {
static barney_is_initialised = 0;
static int Barnie;

if (!barney is initialised) {
Barnie = expensive_function();
barnie_is_initialised = 1;
}
}

Robert
 
E

Emmanuel Delahaye

Alfonso Morra wrote on 03/09/05 :
My func defintion now looks something like this:

int foo(int a, int b) {
static Barney* = expensive_function()
...
}

This code is incorrect. A static is initialised before main() is
called, at a stage where calling functions is not permitted. You want :

int foo(int a, int b)
{
static Barney* = NULL; /* at startup */

if (Barney == NULL)
{
/* the first time (assuming Barney is no more NULL
* after the call of expensive_function() */
Barney* = expensive_function();
}
else
{
/* each other times */
}
}

Any more questions ?

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 
A

Alfonso Morra

Emmanuel said:
Alfonso Morra wrote on 03/09/05 :



This code is incorrect. A static is initialised before main() is called,
at a stage where calling functions is not permitted. You want :

int foo(int a, int b)
{
static Barney* = NULL; /* at startup */

if (Barney == NULL)
{
/* the first time (assuming Barney is no more NULL
* after the call of expensive_function() */
Barney* = expensive_function();
}
else
{
/* each other times */
}
}

Any more questions ?
Nope. This is *exactly* what I was looking for. Thanks
 
K

Keith Thompson

Emmanuel Delahaye said:
Alfonso Morra wrote on 03/09/05 :

This code is incorrect. A static is initialised before main() is
called, at a stage where calling functions is not permitted. You want :

int foo(int a, int b)
{
static Barney* = NULL; /* at startup */

if (Barney == NULL)
{
/* the first time (assuming Barney is no more NULL
* after the call of expensive_function() */
Barney* = expensive_function();
}
else
{
/* each other times */
}
}

Any more questions ?

Yes. What type is Barney?
 
K

Kenneth Brody

Alfonso Morra wrote:
[...]
My func defintion now looks something like this:

int foo(int a, int b) {
static Barney* = expensive_function()

I do not believe that this statement is legal, as a static variable
requires a constant for its initializer. (I assume that the missing
name after "Barney*" is a typo.) My test compiler gives:

foo.c(24) : error C2099: initializer is not a constant
...
}

My question are:

1). Is the expensive function called every, time my function is called
(I can hear a deafening YES to the question - but I'll still ask anyways)

2). Is there a way that I can have this expensive function called once
and ONLY once - the first time the function foo is called.
[...]

If expensive_function() can never return NULL:

static Barney* foo = NULL;

if ( foo == NULL )
foo = expensive_function();

Otherwise:

static int foo_init = 0;
static Barney* foo;

if ( !foo_init )
{
foo = expensive_function();
foo_init = 1;
}
I thought of using bool flags, but its not clar what value they'll be
initialized at (true,false), the first time the function foo is called,
and if I asign an initialization value to a flag, the value will be
initialized to a specific pt, everytime the function is entered, the
flags will get reset - hardly the behaviour I want.

Static variables are not initialized the first time the function is
called. They are initialized at program startup, just as global
variables are initialzied.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 

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

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top