code to run only once, at the first function call

Discussion in 'C Programming' started by bio, Oct 2, 2010.

  1. bio

    bio Guest

    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
    bio, Oct 2, 2010
    #1
    1. Advertising

  2. bio

    Eric Sosman Guest

    On 10/1/2010 9:18 PM, bio wrote:
    > 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 news:comp.programming.threads if
    that's a concern.)

    --
    Eric Sosman
    lid
    Eric Sosman, Oct 2, 2010
    #2
    1. Advertising

  3. bio

    Guest

    On Oct 1, 8:18 pm, bio <> wrote:
    > 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.
    , Oct 2, 2010
    #3
  4. bio

    bio Guest

    On Oct 1, 9:38 pm, "" <>
    wrote:
    > On Oct 1, 8:18 pm, bio <> wrote:
    >
    > > 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.


    thank so much.

    Tuan
    bio, Oct 2, 2010
    #4
  5. bio

    BartC Guest

    "bio" <> wrote in message
    news:...
    > On Oct 1, 9:38 pm, "" <>
    > wrote:
    >> On Oct 1, 8:18 pm, bio <> wrote:


    >> > 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.


    >> 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.


    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.

    >> First, that sort of requirement is often (but not always) a sign of a
    >> flawed design, you may want to rethink it.


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

    --
    Bartc
    BartC, Oct 2, 2010
    #5
  6. "BartC" <> writes:
    > "bio" <> wrote in message
    > news:...
    >> On Oct 1, 9:38 pm, "" <>
    >> wrote:
    >>> On Oct 1, 8:18 pm, bio <> wrote:

    >
    >>> > 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.

    >
    >>> 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.

    >
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 2, 2010
    #6
  7. bio

    BartC Guest

    "Keith Thompson" <> wrote in message
    news:...
    > "BartC" <> writes:


    >>>> On Oct 1, 8:18 pm, bio <> wrote:

    >>
    >>>> > 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.


    >> 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 */


    > 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.

    --
    bartc
    BartC, Oct 2, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nikolaus Rath

    [unittest] Run setUp only once

    Nikolaus Rath, Jul 29, 2008, in forum: Python
    Replies:
    0
    Views:
    292
    Nikolaus Rath
    Jul 29, 2008
  2. Jean-Paul Calderone

    Re: [unittest] Run setUp only once

    Jean-Paul Calderone, Jul 29, 2008, in forum: Python
    Replies:
    2
    Views:
    284
    Roy Smith
    Jul 30, 2008
  3. Jean-Paul Calderone

    Re: [unittest] Run setUp only once

    Jean-Paul Calderone, Jul 29, 2008, in forum: Python
    Replies:
    3
    Views:
    869
    Timothy Grant
    Aug 4, 2008
  4. Oltmans
    Replies:
    1
    Views:
    800
    Francesco Bochicchio
    Sep 28, 2009
  5. Cal Who
    Replies:
    1
    Views:
    450
    Cal Who
    Jun 26, 2010
Loading...

Share This Page