What is the use of static function in C?

V

vishnu

Hi friend,
i have a problem in my program what is the use of static function in C
lang?
plz help me
 
M

Michael Mair

vishnu said:
i have a problem in my program what is the use of static function in C
lang?

Your question is not clear.
What is your problem? "what is the use of static function in C lang?"
seems not to fit the description "problem in [your] program".

If you mean
static int foo (void);
in contrast to
int foo (void);
or
extern int foo (void);
then look up "linkage" in your C textbook.


Cheers
Michael
 
D

Dennis Willson

hi friend,

what is function pointer how it is useful?
plz help me.


hi friend,

what is function pointer how it is useful?
plz help me.

These all look like the same person to me.. I could be wrong, but is someone having wanting answer to their homework problems?
 
M

Malcolm

vishnu said:
Hi friend,
i have a problem in my program what is the use of static function in C
lang?
plz help me
They are hugely useful.

Let's say I want to load a jpeg file. Bascially, all my calling programmer,
Sheila, wants to do is pass in a filename, and get the bits back. She's a 3d
programmer rather than a signals processing type person, and neither knows
nor cares anything about the JPEG format.

So she wants to call

unsigned char *loadjpeg(char *filename, int *width, int *height)

However JPEG files are very complicated - trying to pack the whole loader
into a single function would make the code totally unreadable. There are
also natural units we want to do - for instance the 8-point inverse cosine
transform has got to be called 16 times for each block.

Now let's say I write a function

idct8(float *values)

The problem is that if I make this public, someone might call it. When I go
back to my jpeg loader, i might wnat to make things more efficient. For
instance I might want to alter the function so that it doesn't use floating
point.
The other problem is that Fred, who is doing audio, might also need a idct8
function. In his case the 8 means that it is dealing with 8-bit samples.
So by just declaring the loadjpeg function with external linkage, and making
everything else static, I protect my code, don't pollute namespace, and make
it clear to Sheila that I am contracting to load a jpeg and return the bits,
but I don't provide any math routines or anything else.
 
K

kar1107

Malcolm said:
The other problem is that Fred, who is doing audio, might also need a idct8
function. In his case the 8 means that it is dealing with 8-bit samples.
So by just declaring the loadjpeg function with external linkage, and making
everything else static, I protect my code, don't pollute namespace, and make
it clear to Sheila that I am contracting to load a jpeg and return the bits,
but I don't provide any math routines or anything else.

Another benefit of static is to give additional freedom to compiler to
do optimizations. The compiler knows the function is local to this
translation
unit and can do things like inlining. I remember reading someone
commenting
that it would have been far better for functions to default to static
rather than
global (external linkage). But I guess its too late to change that now
and it is always
better to write new functions as static if possible.

Karthik
 
M

Malcolm

Another benefit of static is to give additional freedom to compiler to
do optimizations. The compiler knows the function is local to this
translation unit and can do things like inlining. I remember reading
someone
commenting that it would have been far better for functions to default to
static
rather than global (external linkage). But I guess its too late to change
that now
and it is always better to write new functions as static if possible.
That is probably a good snapshot of current compiler practise, but
technology changes.
There is no reason a compiler running on a modern PC cannot load the whole
of a largeish program into memory in C source form, and do a full compile,
within a few seconds. In the days when 66Mhz was leading edge, of course you
needed the object file / linker workaround to make compile times acceptable.
(It was the same story with "register". Nowadays it is unusual for the
programmer to be able to make better decisions than the compiler.)

The other point is that runtime efficiency isn't as important as it was.
Most computers nowadays are fast enough, and a good program is one that does
what it says and is easy to use, not one that responds quickly.

Having said that, I have just entered the brave world of Beowulf cluster
massively (well, 40 nodes) parallel supercomputing, and I am resorting to
Fortran to speed things up a bit.
 
F

Flash Gordon

Malcolm said:
That is probably a good snapshot of current compiler practise, but
technology changes.

The reason it is best to write functions as static if possible is not
only because of the compiler, it is also because of the human reader. If
a function is static them you know it is only used locally (unless its
address is taken) which limits how much you have to look at when trying
to fix a bug in it. Otherwise you have to check where else it might be
called from just in case something relies on the behaviour you are about
to change.
There is no reason a compiler running on a modern PC cannot load the whole
of a largeish program into memory in C source form, and do a full compile,
within a few seconds.

That depends on how large the source base is. Loading the source base of
openoffice in to memory and compiling it will take more than a few
seconds on a modern PC.
> In the days when 66Mhz was leading edge, of course you
needed the object file / linker workaround to make compile times acceptable.

If you think that is no longer required now you don't work on large
projects. Try looking up the build time fir a Gentoo Linux system where
everything is built from source.
(It was the same story with "register". Nowadays it is unusual for the
programmer to be able to make better decisions than the compiler.)

That is true.
The other point is that runtime efficiency isn't as important as it was.

That is the attitude that forces people to upgrade hardware. It is also
not true in embedded systems where you might have a limit to the
processing power you can use because of a limit on the amount of heat
you are allowed to dissipate (this applies to at least some avionics
systems).
Most computers nowadays are fast enough, and a good program is one that does
what it says and is easy to use, not one that responds quickly.

A good program is one that does what is required correctly within an
acceptable amount of time and cost an acceptable amount to develop.
These are competing requirements but some things, such as not making the
optimisers job harder than necessary (e.g. declaring functions as
static when they are not called directly from outside the translation
unit) cost very little or nothing but can help meet the performance
requirements.
Having said that, I have just entered the brave world of Beowulf cluster
massively (well, 40 nodes) parallel supercomputing, and I am resorting to
Fortran to speed things up a bit.

Performance will probably always be an issue because people will always
want the software to do more. So you should always make it easy for the
optimiser and maintainer by them the information you have, such as that
a function is only called from within the one translation unit.
 
M

Malcolm

Flash Gordon said:
The reason it is best to write functions as static if possible is not only
because of the compiler, it is also because of the human reader. If a
function is static them you know it is only used locally (unless its
address is taken) which limits how much you have to look at when trying to
fix a bug in it. Otherwise you have to check where else it might be called
from just in case something relies on the behaviour you are about to
change.
That's the real answer. Usually the priority is to have clean,
understandable code. Speed of execution / memory usage / compiling problems
are almost always secondary, except in the inner loops of time-critical
programs.
That depends on how large the source base is. Loading the source base of
openoffice in to memory and compiling it will take more than a few seconds
on a modern PC.
I got a new PC a few weeks ago, and it has 2 GB of memory installed.
1MB of ASCII represents about 100,000 words of English, or an average novel.
That's probably a good year's work for a programmer.
So 2 Gigabytes holds 2000 programmer year's work. That's a large project,
way beyond the resources of all but the largest companies.
If you think that is no longer required now you don't work on large
projects. Try looking up the build time fir a Gentoo Linux system where
everything is built from source.
The reason is that the compiler is designed to treat core as a limited
resource. So it loads each file individually, outputs an object file, then
loads all the object files back in to link them.

There's no reason for a modern compiler to work like that - it can hold all
the source in core for all bu the largest projects. True, you cannot compile
a project with over 2GB of source on an average PC, but given that the 2000
programmer years will cost over a hundred million dollars, a nice
supercomputer hosting a cross compiler won't break the budget.
Performance will probably always be an issue because people will always
want the software to do more. So you should always make it easy for the
^^^^^
optimiser and maintainer by them the information you have, such as that a
function is only called from within the one translation unit.
Usually you are right - there's no point throwing cycles away. However you
cannot declare static functions in Fortran 77, which is the native language
of the Beowulf cluster. So to make all code follow the same conventions is
maybe a good idea - just maybe.
 
Q

quetzalcotl

vishnu said:
Hi friend,
i have a problem in my program what is the use of static function in C
lang?
plz help me

You must avoid using static functions at all costs if there are dynamic
functions available! Use dynamic functions whenever possible.
 
P

pete

You must avoid using static functions
at all costs if there are dynamic
functions available! Use dynamic functions whenever possible.

static functions are a feature of standard C.
"dynamic functions" means nothing in the context of standard C.
 
C

Chris Hills

The other point is that runtime efficiency isn't as important as it was.

Absolutely un-true!
Most computers nowadays are fast enough, and a good program is one that does
what it says and is easy to use, not one that responds quickly.

Most computers on the planet are 8 bit micros with 256 RAM and 64K code
space..... At one time there were 3 or 4 in every PC.

In embedded systems (ie vitally *ANYTHING* that has electrical power
these days) still work on the smaller the memory the better. The cost of
adding a 1 dollar memory chip on to a system can cost 2000 USD in
engineering time and 100,000 USD a year to a project producing 50K units

In a car if you added an additional memory chip to each of the 100 MCU
in the system you will be adding 20,000 USD to the NRE of the car. Then
the productions costs would go up by 200 USD per car...

(all figures for illustration only and are approximate based on 20 years
experience)
 
C

Clark S. Cox III

Did I say it meant anything?

Well, by using it, you certainly implied that it meant something.
Unless, that is, you're in the habit of using meaningless terms.

Perhaps you should clarify what it is yea meant when you said "dynamic
functions".
 
K

Keith Thompson

pete said:
static functions are a feature of standard C.
"dynamic functions" means nothing in the context of standard C.

I think quetzalcotl was trying to provide an appropriate level of help
to someone asking about a homework problem.
 
M

Mark McIntyre

Absolutely un-true!

Even setting aside the microcontroller aspect, this is dangerous, and
I sincerely hope that CS schools don't teach it.

I have a serious problem at work with programmers assuming unlimited
resources - about 2% of our overnight processing now consumes more
than 4GB of memory. If you've ever tried sourcing 16GB of memory for a
new Sun server, you'll know that this is an expensive mistake...
 
Q

quetzalcotl

Clark said:
Well, by using it, you certainly implied that it meant something.
Unless, that is, you're in the habit of using meaningless terms.

I'm in the habit of giving appropriate answers to stupid questions. It
is obvious to me that the OP knows near to nothing about functions in C
or about the effect of the keyword static. In other words, he knows
almost nothing. If he knew about those topics, he could easily infer
what the difference between

int f() { .. }

and

static int f() { ... }

is. It would be another case, if he asked like so: "I know, that static
items (not just functions) cannot be seen from other compilation units.
I just can't imagine how this is useful. Please enlighten me."

Another, albeit less irritating appropriate answer could have been:
"Well, in

static int f(int a, int b) { return a+b; }

I define a static function that computes the sum of two integers, a and
b. This static function can then be used only for that purpose. If, for
example, you want to use a static function to add two doubles, x and y,
you cannot use f. Fortunately, it is possible in most cases to define
another static function that does just what you want to use it for."
 
C

Chris Hills

I'm in the habit of giving appropriate answers to stupid questions. It
is obvious to me that the OP knows near to nothing about functions in C
or about the effect of the keyword static. In other words, he knows
almost nothing. If he knew about those topics,


If he knew he would not be asking. static functions and static variables
are often confused by students and new programmers (and many old ones
too :)

I have noticed that there is a far more vindictive streak in this NG
these days compared to when I started reading it 15 off years ago.

Whilst no one wants to do home work for some one you can always point
them in the right direction of study.

When people come here to ask "the experts" they don't expect them to
behave like self righteous pompous twats.
 
D

David Resnick

I have noticed that there is a far more vindictive streak in this NG
these days compared to when I started reading it 15 off years ago.

Whilst no one wants to do home work for some one you can always point
them in the right direction of study.

When people come here to ask "the experts" they don't expect them to
behave like self righteous pompous twats.

<OT> Yes. Though vague questions much like above, written in exactly
that
style and wording, sometimes seem to be trolls, perhaps by a
single
person (chellapa?). Mostly coming from gmail accounts over
google
groups (yes, I am too)

Some recent examples
I don't think they are worth a real answer -- they
indicate a complete lack of respect for the time of those
being
questioned in that the poster has put no effort into the
matter
themselves and has not tried to find the right place to ask
the question. While rude/cruel responses aren't called for,
neither
are thoughtful answer to thoughtless questions. Perhaps the
correct answer to these posts is just a url:
http://www.catb.org/~esr/faqs/smart-questions.html
</OT>

-David
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top