473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

use of static

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

Jan 16 '06 #1
5 1469
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.
Jan 16 '06 #2

"Superfox il Volpone" <at***@email.it> wrote in message
news:11**********************@g44g2000cwa.googlegr 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];
}
}
Jan 16 '06 #3
pemo wrote:
"Superfox il Volpone" <at***@email.it> wrote in message
news:11**********************@g44g2000cwa.googlegr 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.
Jan 16 '06 #4

"Vladimir S. Oka" <no****@btinternet.com> wrote in message
news:dq**********@nwrdmz03.dmz.ncs.ea.ibs-

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


Jan 16 '06 #5
pemo wrote:
"Vladimir S. Oka" <no****@btinternet.com> wrote in message
news:dq**********@nwrdmz03.dmz.ncs.ea.ibs-
// 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.
Jan 16 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
5
by: Mountain Bikn' Guy | last post by:
How would I do this? public sealed class UtilityClass { public static MyObject Object1;//see note below about importance of static object names in this class public static MyObject Object2;...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
9
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database...
12
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.