3

Possible Duplicate:
C/C++: Static function in header file, what does it mean?
When to put static function definitions in header files in C?

What are the pros and cons of using static function in header file?

Community
  • 1
  • 1
beparas
  • 1,927
  • 7
  • 24
  • 30

1 Answers1

3

Supposing you implement it in the header file, each time your header is included, the function is going to be duplicated. This means heavier produced binary, bad practice and overall nightmare to debug and maintain.

If you just define it in the header, you need to implement in each C file.

EDIT

  • if it's not in the header, you have a function with a given name, implemented more than once, thus providing potentially different behaviours. Otherwise, don't make it static and implement once. Having more than one function with same name is a pitfall for maintainers (thus debug and maintenance hell)
  • static function and inline functions are different matters. Static function are "locale" while inline function are "to be replaced by their body where they are called". In terms of overhead, calling a standard function or a static function is same "performance price".

EDIT 2 Here is the kind of pitfall you can run into

static.h

#ifndef _STATIC_H_
#define  _STATIC_H_
#include <stdio.h>

static void printer(void);
void nonStatic (void);

#endif

a.c

#include "static.h"

static void printer(void)
{
    printf ("half the truth : 21\n");
}


int main (void) {
    printer();
    nonStatic();
}

b.c

#include <stdio.h>
#include "static.h"

static void printer (void)
{
    printf("Truth : 42\n");
}

void nonStatic(void)
{
    printf ("Non static\n");
    printer();
}

Looking at this code, you call "printer" from 2 different locations, you got different behaviour:

D:\temp>gcc -o temp.exe a.c b.c && temp
half the truth : 21
Non static
Truth : 42

Obvious in this small example, really tricky when hidden in a big software

Bruce
  • 7,094
  • 1
  • 25
  • 42
  • What if it's not implemented in the header? Why is it bad practice (are you suggesting inline functions are also bad practice)? Why would it be hard to debug? You need to provide arguments for your statements. (not saying they're wrong) – Luchian Grigore Jul 09 '12 at 07:36
  • True, was a bit quick to answer but I had to maintain code with static variables and functions in headers, it hurts my sensitive feelings :-) Added some arguments, feel free to comment if you think I should put a bit more. – Bruce Jul 09 '12 at 07:44
  • There are advantages though to using statics, both variables and functions. – Luchian Grigore Jul 09 '12 at 07:46
  • A header with include guards would not have the issue you describe. – Tim Post Jul 09 '12 at 07:53
  • Well, as usual, given a certain level of expertise and knowing what you're doing, there can be a gain (thinking of defining debug function/variables with same name but some magic __FILE__ macro e.g.). Given the profile of the person asking, I answered in the most "portable and maintainable code" rather than "code guru" strategy. – Bruce Jul 09 '12 at 07:55
  • Tim : if you define a static function in the header, each C file including the header (and using the function) will have to implement it (static = locale to compilation unit) – Bruce Jul 09 '12 at 07:57