tags:

views:

387

answers:

8

Hi, Is there any chance that Static function can be assessed outside the file scope. ?

+11  A: 

No, unless there's a bug in the compiler. Normally the static function code is not tagged with a name used for exporting the function in the object file, so it is not presented to the linker and it just can't link to it.

This of course only applies to calling the function by name. Other code within the same file can get the function address and pass it into a non-static function in another file and then the function from another file can call your static function.

sharptooth
+4  A: 

Following the standard, a static function cannot be accessed outside of the scope of the file by name because it is subject to internal linkage. It's name is not exported, and not provided to the linker. However, it can still be accessed and called by function pointer, like any other function.

Kornel Kisielewicz
+9  A: 

It could be called from outside the scope via function pointer.

For example, if you had:

static int transform(int x)
{
    return x * 2;
}

typedef int (*FUNC_PTR)(int);

FUNC_PTR get_pointer(void)
{
    return transform;
}

then a function outside the scope can call get_pointer() and use the returned function pointer to call transform.

R Samuel Klatchko
I think that should be "int (*get_pointer(int))(void)".
paxdiablo
@paxdiablo and @caf - thank you.
R Samuel Klatchko
+2  A: 

It depends upon what you mean by "access". Of course, the function cannot be called by name in any other file since it's static in a different file, but you have have a function pointer to it.

$ cat f1.c
/* static */
static int number(void)
{
    return 42;
}

/* "global" pointer */
int (*pf)(void);

void initialize(void)
{
    pf = number;
}
$ cat f2.c
#include <stdio.h>

extern int (*pf)(void);
extern void initialize(void);

int main(void)
{
    initialize();
    printf("%d\n", pf());
    return 0;
}
$ gcc -ansi -pedantic -W -Wall f1.c f2.c
$ ./a.out
42
Alok
+1  A: 

No, the purpose of the keyword static is to limit the scope of the function name to the file.

chrmue
+4  A: 

It cannot be accessed outside a file by it's name. But, you can as well assign it to function pointer and use it wherever you want.

Jay
+2  A: 

Only with trickery. The function is generally not visible to the linker so it won't let you do it.

But, if you provide a function inside the same compilation unit (as the static function) which returns the address of that function:

In main.c:

#inclde <stdio.h>
int (*getGet7(void))(void);
int main (void) {
        int (*fn)(void) = getGet7();
        printf ("Result is: %d\n", fn());
        return 0;
}

In hidden.c:

static int get7 (void) {
        return 7;
}
int (*getGet7(void)) (void) {
        return get7;
}

This will result in the static function get7 being called.

pax> gcc -o demo main.c hidden.c ; ./demo
Result is: 7
paxdiablo
+3  A: 

"Accessed"? It depends on what you mean by this term. I assume when you say "static function" you are talking about standalone function declared static (i.e. declared with internal linkage) as opposed to static class member functions in C++, since the latter are obviosly and easily accessible from anywhere.

Now, a standalone function declared static has internal linkage. It cannot be linked to from any other translation unit. Or, putting it differently, it cannot be referred to by name from any other translation unit. If that's what you meant by "access from outside the file scope", then no, it can't be done.

However, if the other translation units somehow get a pointer to that function (i.e. if you somehow allow that pointer to "leak" into the ouside world), then anybody can still call that function by making an idirect call and thus "access" it. For example, if you declare

static void foo_static(void) {
}

extern void (*foo_ptr)(void) = foo_static;

then in any other translation unit the user will be able to do

extern void (*foo_ptr)(void);
foo_ptr();

and the call will go to your foo_static function. I don't know if that kind of access qualifies as "access" in your question.

AndreyT