static function???
Results 1 to 4 of 4

Thread: static function???

  1. #1
    Senior Member
    Join Date
    May 2003
    Posts
    472

    static function???

    In C,

    static foo()
    {
    ......
    }

    what exactly this means....i mean to say if we declare a function as static what are the spl properties that are aquired by the funcion....
    guru@linux:~> who I grep -i blonde I talk; cd ~; wine; talk; touch; unzip; touch; strip; gasp; finger; mount; fsck; more; yes; gasp; umount; make clean; sleep;

  2. #2
    Senior Member
    Join Date
    Jan 2002
    Posts
    1,207
    A function marked as "static" is not available to the linker when linking a program with more than one module. This is useful because:

    1. A static function can be declared as static in more than one module, and do different things without causing a clash
    2. A static function declared in a library is not exported to the program using it, so it won't clash with another function of the same name, and the program cannot call it (unless they know the address which is highly unlikely)
    3. Your program may build slightly faster and / or smaller because the compiler will decide how to optimise it at compile time rather than leaving it for the linker

    This means that if you have

    main.c containing the function main()
    util.c containing the function wibble() declared static

    In main.c you cannot call wibble() because it is declared as static in a different module.

    If you had a function wobble() declared in main.c and util.c, the linker would give an error. If you declared them both static, it would be fine.

    Edit:

    It is good practice to declare *everything* static unless you're using it in another module of your multi-module program.

    That way, if there are other developers working on some other code, you're less likelly to have name clashes.

    Edit 2:

    You must not declare any functions which are going to be called by name from outside your program (notably main(), WinMain() and similar) as static, or the program won't link

  3. #3
    Senior Member
    Join Date
    Sep 2001
    Posts
    1,027
    Static takes many meanings in C and even more in C++...

    Specifically, in C, declaring a *global* vraiable or a function as static means that it exists only in that module. In other words, that variable/function, declared in mod1.c (for example), cannot be accessed/called from mod2.c. It will also not conflict with vraiables/functions of the same name declared in other modules.

    In the case of a local variable, it means that the variable is "persistent" throughout all calls made to the function in which it is declared (typical example would be to count the number of calls made to that function...)

    In C++ and other OO languages, class variables and methodes declared as static means that they can be accessed without having to instanciate an instance of that class; you can call/access them by ClassName.var or ClassName.methode()...

    Ammo


    Edit: All right, you beat me to it Slarty!
    Credit travels up, blame travels down -- The Boss

  4. #4
    Senior Member
    Join Date
    May 2003
    Posts
    472
    thax a lot abt the information boys
    guru@linux:~> who I grep -i blonde I talk; cd ~; wine; talk; touch; unzip; touch; strip; gasp; finger; mount; fsck; more; yes; gasp; umount; make clean; sleep;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides