question about 'static' definitions

Discussion in 'C Programming' started by Roman Mashak, May 21, 2005.

  1. Roman Mashak

    Roman Mashak Guest

    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:
     
    Roman Mashak, May 21, 2005
    #1
    1. Advertising

  2. Roman Mashak

    Michael Mair Guest

    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.
     
    Michael Mair, May 21, 2005
    #2
    1. Advertising

  3. Roman Mashak

    CBFalconer Guest

    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
     
    CBFalconer, May 21, 2005
    #3
  4. Roman Mashak

    Malcolm Guest

    "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?
    >

    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.
     
    Malcolm, May 21, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Barth

    Question about definitions

    Thomas Barth, Nov 15, 2004, in forum: C++
    Replies:
    3
    Views:
    2,625
    Victor Bazarov
    Nov 15, 2004
  2. Rakesh Sinha
    Replies:
    4
    Views:
    1,879
    Rakesh Sinha
    Jan 13, 2005
  3. LuB

    static definitions

    LuB, May 28, 2005, in forum: C++
    Replies:
    2
    Views:
    284
  4. ark

    A question on incomplete definitions

    ark, Dec 17, 2003, in forum: C Programming
    Replies:
    24
    Views:
    890
    Peter Shaggy Haywood
    Dec 22, 2003
  5. Jaco Naude
    Replies:
    1
    Views:
    371
    Alf P. Steinbach
    Jul 3, 2009
Loading...

Share This Page