use of static

  • Thread starter Superfox il Volpone
  • Start date
S

Superfox il Volpone

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 :)
 
V

Vladimir S. Oka

Superfox said:
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
 
P

pemo

Superfox il Volpone said:
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];
}
}
 
V

Vladimir S. Oka

pemo said:
Superfox il Volpone said:
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
 
P

pemo

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

Vladimir S. Oka

pemo said:
// 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... ;-)
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top