Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > use of static

Reply
Thread Tools

use of static

 
 
Superfox il Volpone
Guest
Posts: n/a
 
      01-16-2006
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

 
Reply With Quote
 
 
 
 
Vladimir S. Oka
Guest
Posts: n/a
 
      01-16-2006
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.
 
Reply With Quote
 
 
 
 
pemo
Guest
Posts: n/a
 
      01-16-2006

"Superfox il Volpone" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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];
}
}


 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      01-16-2006
pemo wrote:
> "Superfox il Volpone" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>> 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.
 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      01-16-2006

"Vladimir S. Oka" <(E-Mail Removed)> wrote in message
news:dqgobq$smh$(E-Mail Removed)-

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




 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      01-16-2006
pemo wrote:
> "Vladimir S. Oka" <(E-Mail Removed)> wrote in message
> news:dqgobq$smh$(E-Mail Removed)-
>
>>> // 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PIX public/24 ip static mapping means 256 times interfaces static maps? Nieuws Xs4all Cisco 2 05-26-2005 06:25 PM
PIX public/24 ip static mapping means 256 times interfaces static maps? Nieuws Xs4all Cisco 0 05-26-2005 11:07 AM
Static is REALLY Static! Paul W ASP .Net 2 05-03-2005 10:12 AM
Static classes with static members Ben ASP .Net 3 06-01-2004 07:43 PM
Static vs. non-static connection Natan ASP .Net 8 05-26-2004 08:21 AM



Advertisments