473,399 Members | 2,159 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,399 software developers and data experts.

question about 'static' definitions

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: mr*@tusur.ru
Nov 14 '05 #1
3 1180
Roman Mashak wrote:
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #2
Roman Mashak wrote:

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:
news:news.announce.newusers
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

Nov 14 '05 #3

"Roman Mashak" <mr*@tusur.ru> wrote

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.
Nov 14 '05 #4

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

Similar topics

23
by: Serve La | last post by:
When I write a medium to large sized C application I tend to create structures first, then makeStruct and freeStruct functions that dynamically allocate/free the struct and then I create a bunch of...
24
by: ark | last post by:
Hello group, Could you help me with this: static const int x; ............ something ............. static const int x = 17; It looks perfectly legal to me but MSVC/C++ 6.0 gives, on the...
5
by: jchludzinski | last post by:
I have 3 files (see below: a.h, w.c, ww.c). I would like to use a single x (declared somewhere) which would global to both compilation units: w.c & ww.c. No matter where I place the "extern"...
7
by: jois.de.vivre | last post by:
Hi, I'm having some linker issues when using static variables in a class. class TestClass { public: TestClass() { Count++;
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
32
by: lcdgoncalves | last post by:
Hi everyone Is there a real need to use keyword static with functions, if we simply don't declare their prototypes in .h file? Many textbooks avoid to discuss this matter and/or discuss only...
9
by: wizwx | last post by:
There are two typical implementations of a singleton. The first one is to use a static pointer class Singleton { static Singleton * pSingleton; public: static Singleton * instance() {...
5
by: Hong Ye | last post by:
Traits is a useful template technique to simplfy the implementation of some classes. however, I met some questions when I tried to understand and implement it. Following is an example of traits...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
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: 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:
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
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,...
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.