question about 'static' definitions

R

Roman Mashak

Hello, All!

As far as I understand, function declaration with 'static' keyword
limits the access to these functions within the file where it declared. So,
my question is: where can it be used in real applications?

Thanks in advance.

With best regards, Roman Mashak. E-mail: (e-mail address removed)
 
M

Michael Mair

Roman said:
Hello, All!

As far as I understand, function declaration with 'static' keyword
limits the access to these functions within the file where it declared. So,
my question is: where can it be used in real applications?

In every single translation unit.

For example, if you decide to have essentially "one function per module"
then this usually means that you have one "visible" function, i.e.
a function with external linkage which can be called from "everywhere".
Now, this function may become too large or may contain non-portable
parts, so you split off other functions. These functions reside within
the same translation unit but usually are not useful out of context or
should not be accessible from without as this would enable easy misuse
of the module. So, you do not want these functions to be "visible".
Using 'static' gives them internal linkage, et voila.
Another point are identifiers -- if you do not broadcast the name of
all functions into the global namespace, then you are less likely
to run into name clashes. This is, for example, important for
libraries; all the functions not intended for the user should not
pollute the namespace.
This becomes obvious if there are two libraries which both have the
externally useless function my_little_helper() and give you trouble
as they cannot be used together reliably.

So, the question really is: When do you _not_ want your functions to
have internal linkage?


Cheers
Michael
 
C

CBFalconer

Roman said:
As far as I understand, function declaration with 'static' keyword
limits the access to these functions within the file where it
declared. So, my question is: where can it be used in real
applications?
From within the file within which it is declared. No prototype
should appear in the corresponding .h header.

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
M

Malcolm

Roman Mashak said:
As far as I understand, function declaration with 'static' keyword
limits the access to these functions within the file where it declared.
So, my question is: where can it be used in real applications?
It very frequently happens that you only want to expose certian functions to
the outside world.
For instance one of my files does clustering. It uses the kmeans algorithm,
so I want the caller to be able to call the function called kmeans().
Internally the algorithm needs a cluster structure that conints a list of
members and centroids, which the caller doesn't need to know about. Also, it
doesn't make sense for him to call any of the subroutines directly.
By making the subroutines "static" I can use a simple name like "assign"
without any danger of a name clash, and also I know that I can modify the
functions to my heart's content, as long as I still provide a list of
clusters for the call to kmeans(), without breaking anything else.
 

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

Similar Threads

tolower() and toupper() 8
re-definition of standard types 9
get random number in range [10..50] 14
buffer overflow 20
fgets() question 3
using '__' in names 19
missing initializer 4
lint 4

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top