14

Is there any chance that a function defined with static can be accessed outside the file scope?

1
  • 8
    If you put the function in the header file. Then there will be a static version in each compilation unit. Feb 2, 2010 at 8:50

8 Answers 8

22

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 can 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
16

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.

0
12

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.

6

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.

5

"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.

4

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.

3

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
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.