Thread: Static functions for data hiding.

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    34

    Static functions for data hiding.

    Hi everyone, just curious. When creating C files that interact with other C files, normally this is managed through the .h file(s).

    By excluding function declaration prototypes from the .h file(s) they can't be accessed/used outside of that file. (or as far as I am aware). So, my question is, why use static to mean private to this source file? isn't it overkill?

    The only possible reason (might be) so that it is 'clear' when reading a source file which functions won't have a declaration prototype in the .h file.

    Am I missing something important?

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    You also avoid name space pollution.

    Consider, for example, a project where you have several .c files that have a helper function named process() they use internally. If they are not declared as static, it will create a link time conflict; the linker won't know (but will probably try to guess, with varying results) which process() is being called where. (The header files are not examined at link time, so it does not matter whether the functions are declared in the header file or not; only whether they're static/local or not.)

    This is also why most C libraries name their public functions library_function().

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    34
    Quote Originally Posted by Nominal Animal View Post
    You also avoid name space pollution.

    Consider, for example, a project where you have several .c files that have a helper function named process() they use internally. If they are not declared as static, it will create a link time conflict; the linker won't know (but will probably try to guess, with varying results) which process() is being called where. (The header files are not examined at link time, so it does not matter whether the functions are declared in the header file or not; only whether they're static/local or not.)

    This is also why most C libraries name their public functions library_function().
    So, let me make sure I have this right.

    If I have two source files, both have a function called process().. even though they are helper functions and not included in any header file, there will be a naming clash?

    If my understanding (thanks very much to your explanation) by using static functions I wouldn't have that problem?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Lawson
    If I have two source files, both have a function called process().. even though they are helper functions and not included in any header file, there will be a naming clash?
    Yes. The technical reason is that by default, function names have external linkage, hence they must be unique across the program. Declaring them static changes the linkage to internal linkage, hence they only need to be unique to each translation unit (i.e., headers + source file).

    Quote Originally Posted by Lawson
    If my understanding (thanks very much to your explanation) by using static functions I wouldn't have that problem?
    Yes, unless you happen to define two static functions named process in the same translation unit.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    34
    Quote Originally Posted by laserlight View Post
    Yes. The technical reason is that by default, function names have external linkage, hence they must be unique across the program. Declaring them static changes the linkage to internal linkage, hence they only need to be unique to each translation unit (i.e., headers + source file).


    Yes, unless you happen to define two static functions named process in the same translation unit.
    When you say translation unit, you just mean two static functions with the same name inside the same source file?

    Never mind, you already answered that question!

    Thank you laserLight.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static functions
    By ashaikh432 in forum C# Programming
    Replies: 15
    Last Post: 08-27-2010, 03:22 PM
  2. namespace and data hiding
    By Tux0r in forum C++ Programming
    Replies: 20
    Last Post: 07-09-2009, 05:00 PM
  3. Replies: 10
    Last Post: 06-17-2009, 04:20 PM
  4. Calling non-static functions on static variables
    By pandu in forum C++ Programming
    Replies: 14
    Last Post: 06-19-2008, 03:07 AM
  5. static and non-static data members
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-16-2002, 10:06 AM