code to run only once, at the first function call

B

bio

Hi,
I have a function, in which some lines are designed to initiate the
data at the first function call only. The following calls to this
function won't invoke these lines. Could someone tell me how to do
this.

Thanks,
Tuan
 
E

Eric Sosman

Hi,
I have a function, in which some lines are designed to initiate the
data at the first function call only. The following calls to this
function won't invoke these lines. Could someone tell me how to do
this.

One way is to use a static variable:

double my_sqrt(double value) {
static int been_here_before = 0;
if (!been_here_before) {
printf ("My very first square root is of %g\n", value);
been_here_before = 1;
}
return sqrt(value);
}

(Off-topic: This is fine for single-threaded programs, but fails
in multi-threaded contexts. See if
that's a concern.)
 
R

robertwessel2

Hi,
  I have a function, in which some lines are designed to initiate the
data at the first function call only. The following calls to this
function won't invoke these lines. Could someone tell me how to do
this.


First, that sort of requirement is often (but not always) a sign of a
flawed design, you may want to rethink it. Consider, for example,
what would happen if your application wanted two or more instances of
whatever resource the function in question represents.

But the easiest way is to just have a static variable in the function
with an initializer to clear it, and then test it at entry. If it's
clear, do the initialization, and then set the flag.
 
B

bio

First, that sort of requirement is often (but not always) a sign of a
flawed design, you may want to rethink it.  Consider, for example,
what would happen if your application wanted two or more instances of
whatever resource the function in question represents.

But the easiest way is to just have a static variable in the function
with an initializer to clear it, and then test it at entry.  If it's
clear, do the initialization, and then set the flag.

thank so much.

Tuan
 
B

BartC

bio said:
thank so much.

Sometimes it's better to explicitly initialise the data, by signalling this
via one of the arguments. For example:

fn(-1); /* tell fn to initialise */

fn(n); /* ordinary call */

Of course you need to choose a value that would not normally be passed (as
in -1 here).

This can have better control, and you can also re-initialise later.

(I think he's suggesting that the function shouldn't store it's own internal
state, but it should be provided by the caller.)
 
K

Keith Thompson

BartC said:
Sometimes it's better to explicitly initialise the data, by signalling this
via one of the arguments. For example:

fn(-1); /* tell fn to initialise */

fn(n); /* ordinary call */

Of course you need to choose a value that would not normally be passed (as
in -1 here).

This can have better control, and you can also re-initialise later.
[...]

That could work (assuming a distinguished value is available),
but I wouldn't do it that way.

Instead, I'd provide a separate initialization function that needs
to be called before calling fn(). The caller can then write:

init_fn();
fn(n);

and not have to worry about the fact that I've chosen -1, for some
reason, as a value that makes fn() behave completely differently
than it normally does.

Or I might provide a reset_fn() function to performs the
initialization explicitly, and have fn() itself use a static object
in fn() so it can implicitly initialize itself on the first call
(as others have suggested here). This guarantees that fn() is
never called without first being initialized, but allows you to
reinitialize if necessary. It keeps the normal case as simple
as possible while permitting other cases.

And if more functions depend on the initialization, the static
data can be moved to file scope (in a separately compiled .c file
so it's still invisible to outside callers), and each function can
check it before doing its thing.
 
B

BartC

That could work (assuming a distinguished value is available),
but I wouldn't do it that way.

I assumed whatever was being initialised was in some way local to the
function.

Otherwise there are other possibilities as you've explained.
Or I might provide a reset_fn() function to performs the
initialization explicitly, and have fn() itself use a static object
in fn() so it can implicitly initialize itself on the first call

Of course, things might be so simple: perhaps there are many call sites for
the function, and it's not known which will be called first. Yet no
initialisation (creating a file for example) should be done until at least
one call is made. Then an explicit initialisation call may not be practical,
and your second approach is better.
 

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
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top