473,800 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(in t 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(yea r)) 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(in t 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(yea r)) 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_showTh isTable(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 1482
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(in t 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(yea r)) 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(in t 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(yea r)) 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_showTh isTable(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.goo glegroups.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(in t 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(yea r)) 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(in t 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(yea r)) 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(in t 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(yea r) && 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.goo glegroups.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(in t 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(yea r)) 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(in t 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(yea r)) 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(in t 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(yea r) && 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(in t 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(yea r) && 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****@btinter net.com> wrote in message
news:dq******** **@nwrdmz03.dmz .ncs.ea.ibs-

// Only special case.
if(leapYear(yea r) && 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(in t 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(yea r) && 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(yea r) && 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****@btinter net.com> wrote in message
news:dq******** **@nwrdmz03.dmz .ncs.ea.ibs-
// Only special case.
if(leapYear(yea r) && 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(in t 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(yea r) && 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(yea r) && 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
4596
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: VarArray::funct were an extern, but it is declared in the same file (q.v.). What is the remedy for this? =================
15
6596
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 either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local declarations). Any static member function like this: //accessing static member i static void...
5
1925
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; // ... public static MyObject Object400;
3
2088
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 copy of data as opposed to multiple instances of the class) C# is now applying the static concept to methods. Why?, I thought that only one copy of the methods were loaded into memory not matter how many instances were created. Is this different...
9
2666
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 operations on purchase orders and inventory. I have created a PurchaseOrder class and Inventory class to encapsulate operations like creating POs, finding items, etc. These two classes are used extensively from different parts of the app.
12
2589
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 advance, Joe
55
6254
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 C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
1
3538
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 following (with incomplete succes): I want in a given namespace Parameters a list of "initializers" (which are objects derived from a simple interface that can be implemented anywhere, and are used to define which parameters the program will take at...
9
5850
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 the static Parse method of the conversion class. if (InValue is string) return T.Parse((string)InValue); else return base.ConvertFrom(context, culture, InValue);
14
6029
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 static inside functions (i.e. local static objects) 5. objects declared at file scope.
0
9690
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10275
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.