use of static

Discussion in 'C Programming' started by Superfox il Volpone, Jan 16, 2006.

  1. I read in the glibc documentation that using 'static' in a function can
    save the content of a variable.
    So I want to use it to write a function that return the number of days
    in one month. Since the days on one month are even the some, it could
    be better to compute the array only one time. And so I did :

    int nDaysOnMonth(int month, int year){
    static int i_dmonths[12];
    i_dmonths[0] = i_dmonths[2] = i_dmonths[4] = i_dmonths[6] =
    i_dmonths[7] = i_dmonths[9] = i_dmonths[11] = 31;
    i_dmonths[3] = i_dmonths[5] = i_dmonths[8] = i_dmonths[10] = 30;
    if(leapYear(year)) i_dmonths[1] = 29;
    else i_dmonths[1] = 28;
    return i_dmonths[month-1];
    }

    The function works but my doubt is if it remakes the array each time
    that I call the function. If it's so, it's correct the use of a global
    variable to understand if the function was initialized before or never.
    For example :

    bool isInit = false; // global var
    int nDaysOnMonth(int month, int year){
    if(!isInit){
    static int i_dmonths[12];
    i_dmonths[0] = i_dmonths[2] = i_dmonths[4] = i_dmonths[6] =
    i_dmonths[7] = i_dmonths[9] = i_dmonths[11] = 31;
    i_dmonths[3] = i_dmonths[5] = i_dmonths[8] = i_dmonths[10] = 30;
    } // end if (!isInit)
    if(leapYear(year)) i_dmonths[1] = 29;
    else i_dmonths[1] = 28;
    return i_dmonths[month-1];
    }

    .... or the compiler could leave some error ?!


    Another question is that I have some list with some events for days and
    months and I want to stamp on screen a table with all events for one
    month, for each months. So I thought to make a function that contains
    an array of 31 list elements (one cell for day). This function will
    called more times on the program. So I did something like :

    void function_showThisTable(int i_month, int i_year, other arguments
    (LISTS)){
    // DAYTABLE is a struct, the type of element on the lists...
    DAYTABLE** tabs = calloc(31, sizeof(DAYTABLE*));
    // etc...
    }

    1. Could I insert the static before DAYTABLE** (the table is ever of 31
    elements, have I to realloc each time the space ?!), if yes hhave I to
    use a 'global variable' to understand if the function was inizialized
    one time ?!
    2. The use of calloc assicure that each pointer (DAYTABLE*) will be
    NULL at the initialization ?! ... or have I to do a loop to assign each
    cell like NULL.
    3. The compiler understands that I want to make an array of pointers or
    could give some error if I try to go in some row like tabs[3] or
    tabs[5] ?!

    Thanks for any answer.

    Bye
    - Atari

    p.s. I'm sorry for my bad english, I'm trying to learn it too :)
     
    Superfox il Volpone, Jan 16, 2006
    #1
    1. Advertising

  2. Superfox il Volpone wrote:
    > I read in the glibc documentation that using 'static' in a function can
    > save the content of a variable.
    > So I want to use it to write a function that return the number of days
    > in one month. Since the days on one month are even the some, it could
    > be better to compute the array only one time. And so I did :
    >
    > int nDaysOnMonth(int month, int year){
    > static int i_dmonths[12];
    > i_dmonths[0] = i_dmonths[2] = i_dmonths[4] = i_dmonths[6] =
    > i_dmonths[7] = i_dmonths[9] = i_dmonths[11] = 31;
    > i_dmonths[3] = i_dmonths[5] = i_dmonths[8] = i_dmonths[10] = 30;
    > if(leapYear(year)) i_dmonths[1] = 29;
    > else i_dmonths[1] = 28;
    > return i_dmonths[month-1];
    > }
    >
    > The function works but my doubt is if it remakes the array each time
    > that I call the function. If it's so, it's correct the use of a global


    Yes, it does "remake" the whole thing every time you invoke it.

    > variable to understand if the function was initialized before or never.
    > For example :
    >
    > bool isInit = false; // global var
    > int nDaysOnMonth(int month, int year){
    > if(!isInit){
    > static int i_dmonths[12];
    > i_dmonths[0] = i_dmonths[2] = i_dmonths[4] = i_dmonths[6] =
    > i_dmonths[7] = i_dmonths[9] = i_dmonths[11] = 31;
    > i_dmonths[3] = i_dmonths[5] = i_dmonths[8] = i_dmonths[10] = 30;
    > } // end if (!isInit)
    > if(leapYear(year)) i_dmonths[1] = 29;
    > else i_dmonths[1] = 28;
    > return i_dmonths[month-1];
    > }


    First point: if you're OK with isInit as a global, why don't you make
    days-in-a-month array global as well. It's actually much better, as
    isInit can mean too many different things, while number of days in a
    month is hardly going to change on this planet (yes, I know it differs
    for some here as well...).

    There most likely are many better solutions to your problem but, why not:

    int i_dmonths[] = {31, 28, /* fill in the others... */, 30, 31};

    And then, whenever you get a new 'year', the first thing you do is:

    if (leapYear(year))
    {
    i_dmonths[1] = 29;
    }
    else
    {
    i_dmonths[1] = 28;
    }

    Or some-such...

    In your solution, you'd have to call nDaysOnMonth() whenever year
    changes anyway, so why not simplify it to the function with just the if
    statement above?

    >
    > .... or the compiler could leave some error ?!


    Didn't look for this...

    >
    >
    > Another question is that I have some list with some events for days and
    > months and I want to stamp on screen a table with all events for one
    > month, for each months. So I thought to make a function that contains
    > an array of 31 list elements (one cell for day). This function will
    > called more times on the program. So I did something like :


    Hopefully, someone else will look into this. I have a {l/w}ife to go
    back to now... ;-)

    >
    > void function_showThisTable(int i_month, int i_year, other arguments
    > (LISTS)){
    > // DAYTABLE is a struct, the type of element on the lists...
    > DAYTABLE** tabs = calloc(31, sizeof(DAYTABLE*));
    > // etc...
    > }
    >
    > 1. Could I insert the static before DAYTABLE** (the table is ever of 31
    > elements, have I to realloc each time the space ?!), if yes hhave I to
    > use a 'global variable' to understand if the function was inizialized
    > one time ?!
    > 2. The use of calloc assicure that each pointer (DAYTABLE*) will be
    > NULL at the initialization ?! ... or have I to do a loop to assign each
    > cell like NULL.
    > 3. The compiler understands that I want to make an array of pointers or
    > could give some error if I try to go in some row like tabs[3] or
    > tabs[5] ?!
    >
    > Thanks for any answer.
    >
    > Bye
    > - Atari
    >
    > p.s. I'm sorry for my bad english, I'm trying to learn it too :)
    >


    Cheers

    Vladimir

    --
    My e-mail address is real, and I read it.
     
    Vladimir S. Oka, Jan 16, 2006
    #2
    1. Advertising

  3. Superfox il Volpone

    pemo Guest

    "Superfox il Volpone" <> wrote in message
    news:...
    >I read in the glibc documentation that using 'static' in a function can
    > save the content of a variable.
    > So I want to use it to write a function that return the number of days
    > in one month. Since the days on one month are even the some, it could
    > be better to compute the array only one time. And so I did :
    >
    > int nDaysOnMonth(int month, int year){
    > static int i_dmonths[12];
    > i_dmonths[0] = i_dmonths[2] = i_dmonths[4] = i_dmonths[6] =
    > i_dmonths[7] = i_dmonths[9] = i_dmonths[11] = 31;
    > i_dmonths[3] = i_dmonths[5] = i_dmonths[8] = i_dmonths[10] = 30;
    > if(leapYear(year)) i_dmonths[1] = 29;
    > else i_dmonths[1] = 28;
    > return i_dmonths[month-1];
    > }
    >
    > The function works but my doubt is if it remakes the array each time
    > that I call the function. If it's so, it's correct the use of a global
    > variable to understand if the function was initialized before or never.
    > For example :
    >
    > bool isInit = false; // global var
    > int nDaysOnMonth(int month, int year){
    > if(!isInit){
    > static int i_dmonths[12];
    > i_dmonths[0] = i_dmonths[2] = i_dmonths[4] = i_dmonths[6] =
    > i_dmonths[7] = i_dmonths[9] = i_dmonths[11] = 31;
    > i_dmonths[3] = i_dmonths[5] = i_dmonths[8] = i_dmonths[10] = 30;
    > } // end if (!isInit)
    > if(leapYear(year)) i_dmonths[1] = 29;
    > else i_dmonths[1] = 28;
    > return i_dmonths[month-1];


    <snip>

    Just the first bit - dinner on the table!

    int nDaysOnMonth(int month, int year)
    {
    static int i_dmonths[12] = {31, 31, 31, 30, 31, 30, 31, 31, 30, 31, 30,
    31};

    // Only special case.
    if(leapYear(year) && month == 2)
    {
    return 29;
    }
    else
    {
    return i_dmonths[month - 1];
    }
    }
     
    pemo, Jan 16, 2006
    #3
  4. pemo wrote:
    > "Superfox il Volpone" <> wrote in message
    > news:...
    >> I read in the glibc documentation that using 'static' in a function can
    >> save the content of a variable.
    >> So I want to use it to write a function that return the number of days
    >> in one month. Since the days on one month are even the some, it could
    >> be better to compute the array only one time. And so I did :
    >>
    >> int nDaysOnMonth(int month, int year){
    >> static int i_dmonths[12];
    >> i_dmonths[0] = i_dmonths[2] = i_dmonths[4] = i_dmonths[6] =
    >> i_dmonths[7] = i_dmonths[9] = i_dmonths[11] = 31;
    >> i_dmonths[3] = i_dmonths[5] = i_dmonths[8] = i_dmonths[10] = 30;
    >> if(leapYear(year)) i_dmonths[1] = 29;
    >> else i_dmonths[1] = 28;
    >> return i_dmonths[month-1];
    >> }
    >>
    >> The function works but my doubt is if it remakes the array each time
    >> that I call the function. If it's so, it's correct the use of a global
    >> variable to understand if the function was initialized before or never.
    >> For example :
    >>
    >> bool isInit = false; // global var
    >> int nDaysOnMonth(int month, int year){
    >> if(!isInit){
    >> static int i_dmonths[12];
    >> i_dmonths[0] = i_dmonths[2] = i_dmonths[4] = i_dmonths[6] =
    >> i_dmonths[7] = i_dmonths[9] = i_dmonths[11] = 31;
    >> i_dmonths[3] = i_dmonths[5] = i_dmonths[8] = i_dmonths[10] = 30;
    >> } // end if (!isInit)
    >> if(leapYear(year)) i_dmonths[1] = 29;
    >> else i_dmonths[1] = 28;
    >> return i_dmonths[month-1];

    >
    > <snip>
    >
    > Just the first bit - dinner on the table!
    >
    > int nDaysOnMonth(int month, int year)
    > {
    > static int i_dmonths[12] = {31, 31, 31, 30, 31, 30, 31, 31, 30, 31, 30,
    > 31};
    >
    > // Only special case.
    > if(leapYear(year) && month == 2)
    > {
    > return 29;
    > }
    > else
    > {
    > return i_dmonths[month - 1];
    > }
    > }
    >
    >


    First, in my previous reply I completely failed to note that the OP
    wanted a function that returns number of days in the month, hence my
    waffling about i_dmonths being made global. :-(

    Second, February has 28 or 29 days. The above (from pemo) would return
    31 on any non-leap year. I guess he has as fast fingers, as I have
    eyes... So, I'd say:

    int nDaysOnMonth(int month, int year)
    {
    static int i_dmonths[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
    30, 31};

    // Only special case.
    if(leapYear(year) && month == 2)
    {
    return 29;
    }
    else
    {
    return i_dmonths[month - 1];
    }
    }

    Cheers

    Vladimir


    --
    My e-mail address is real, and I read it.
     
    Vladimir S. Oka, Jan 16, 2006
    #4
  5. Superfox il Volpone

    pemo Guest

    "Vladimir S. Oka" <> wrote in message
    news:dqgobq$smh$-

    >>
    >> // Only special case.
    >> if(leapYear(year) && month == 2)
    >> {
    >> return 29;
    >> }
    >> else
    >> {
    >> return i_dmonths[month - 1];
    >> }
    >> }
    >>
    >>

    >
    > First, in my previous reply I completely failed to note that the OP wanted
    > a function that returns number of days in the month, hence my waffling
    > about i_dmonths being made global. :-(
    >
    > Second, February has 28 or 29 days. The above (from pemo) would return 31
    > on any non-leap year. I guess he has as fast fingers, as I have eyes...
    > So, I'd say:
    >
    > int nDaysOnMonth(int month, int year)
    > {
    > static int i_dmonths[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
    > 31};
    >
    > // Only special case.
    > if(leapYear(year) && month == 2)
    > {
    > return 29;
    > }
    > else
    > {
    > return i_dmonths[month - 1];
    > }
    > }


    Yes, fingers too fast - brain not in gear - dinner getting cold. Thanks for
    the correction.

    Over 'pud' I also thought I should have put ...

    if(month == 2 && leapYear(year))

    instead of

    if(leapYear(year) && month == 2)

    The OP seems to be wanting to optimise the pants off this, so I guess the
    overhead of the function call should be left until needed.
     
    pemo, Jan 16, 2006
    #5
  6. pemo wrote:
    > "Vladimir S. Oka" <> wrote in message
    > news:dqgobq$smh$-
    >
    >>> // Only special case.
    >>> if(leapYear(year) && month == 2)
    >>> {
    >>> return 29;
    >>> }
    >>> else
    >>> {
    >>> return i_dmonths[month - 1];
    >>> }
    >>> }
    >>>
    >>>

    >> First, in my previous reply I completely failed to note that the OP wanted
    >> a function that returns number of days in the month, hence my waffling
    >> about i_dmonths being made global. :-(
    >>
    >> Second, February has 28 or 29 days. The above (from pemo) would return 31
    >> on any non-leap year. I guess he has as fast fingers, as I have eyes...
    >> So, I'd say:
    >>
    >> int nDaysOnMonth(int month, int year)
    >> {
    >> static int i_dmonths[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
    >> 31};
    >>
    >> // Only special case.
    >> if(leapYear(year) && month == 2)
    >> {
    >> return 29;
    >> }
    >> else
    >> {
    >> return i_dmonths[month - 1];
    >> }
    >> }

    >
    > Yes, fingers too fast - brain not in gear - dinner getting cold. Thanks for
    > the correction.
    >
    > Over 'pud' I also thought I should have put ...
    >
    > if(month == 2 && leapYear(year))
    >
    > instead of
    >
    > if(leapYear(year) && month == 2)
    >
    > The OP seems to be wanting to optimise the pants off this, so I guess the
    > overhead of the function call should be left until needed.
    >
    >
    >
    >


    Hmmm, just before the pud (no quotes; yes, I seem to be on the same
    islet -- an enjoying it), if OP wants to be safer:

    2 == month

    is the way to go...

    Cheers

    Vladimir

    PS
    For optimising the pants off this, I believe we need to sleep over our
    collective puds... ;-)

    --
    My e-mail address is real, and I read it.
     
    Vladimir S. Oka, Jan 16, 2006
    #6
    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. Natan

    Static vs. non-static connection

    Natan, May 24, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    7,330
    Sami Vaaraniemi
    May 26, 2004
  2. Ben
    Replies:
    3
    Views:
    420
  3. Paul W

    Static is REALLY Static!

    Paul W, May 3, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    374
  4. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    Why do static and non-static method names collide?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 2, 2003, in forum: Java
    Replies:
    12
    Views:
    5,647
    cgbusch
    Jul 5, 2003
  5. Gary
    Replies:
    1
    Views:
    3,987
    Ryan Stewart
    Jan 16, 2004
Loading...

Share This Page