[ back to toc ]

static function

Date: 2002/02/20 11:59

Q:
hi

I know about scope rule using static for varialbes but am confused in
function.

for example, some function is
static int name(int ..){...} or
static void name(int ..){...} ,then

how does scope work? As I know, that function is only called from the
functions in the same file. is that true?
A:
The keyword static is used for two different thing in C and this is really
confusing. So do not feel ashamed you are confused, it is not your fault.

When a function is static the compiler generates an object code (.o or
.obj file depending on the operating system being some flavour of UNIX or
Windows NT) that does NOT contain the name of this function in the object
file symbol table. This symbol table is used by the linker to link object
files together into executable.

You can use static functions that you want to be prive inside the source
file. So it is true.

Regards,
Peter

*******************
Please take your time and rate me at AllExperts.com
I ask you to do that the way you believe I deserve.
This is a feedback that I get from allexperts but
it does not affect my life in any financial way. This
is just a mental feedback.

In case you think my answer was really good and if you feel
it appropriate do not hesitate to nominate me to volunteer
of the month.

I do this job volunteer in my free time and this is all I get
as compensation.
Q:
Thnak for your answers.

I have a folowup question. I am debuging some source files and a static
function is called by somthing in other files, which is not true for
static function as you said. This sources are coded using IPC and
event-driven way and that would make it possible but I don't clearly
understand.

this is the example.

static void status foo(msg_handle *h, void *priv)
{

RSP_MSG(h,SUCCESS) // this could be somthing retuning value
retunn REQ_REPLY
}

That could be called by RSQ_MSG in other source file. Actually I don't
know well about event-driven way, event-handler, handle registration,
callback function, which makes me frustrated in analyzing above function.
Would you explain in detail? Thanks in advance.

A:
In my last letter I wrote that the name of the static function does not
get into the symbol table of the object file and therefore other functions
implemented in other source files and compiled into other object files can
not find the static function when the linker puts all the object files
together.

However the function is there and in case someone knows the entry point,
the address where the function starts can execute a machine code calling
the function. The question is how this can happen in case of a static
function? One solution is that the calling function is implemented in the
same source file as the static function. The other possibility is that the
calling function is implemented in another source function and calls the
static function indirectly. For example there is a global variable of some
pointer type, which is set by some code (implemented in the file where the
static function is) to point to the entry point of the static function.
Using this variable the calling function can call the static function
without knowing the name of the function.

I hope it helps.

Regards,
Peter

*******************
Please take your time and rate me at AllExperts.com
I ask you to do that the way you believe I deserve.
This is a feedback that I get from allexperts but
it does not affect my life in any financial way. This
is just a mental feedback.

In case you think my answer was really good and if you feel
it appropriate do not hesitate to nominate me to volunteer
of the month.

I do this job volunteer in my free time and this is all I get
as compensation.

[ back to toc ]