How can I access a shadowed global variable in C?

2020-01-23 04:05发布

How can I access a shadowed global variable in C? In C++ I can use :: for the global namespace.

7条回答
成全新的幸福
2楼-- · 2020-01-23 04:21

If you are talking about shadowed global var, then (on Linux) you can use dlsym() to find an address of the global variable, like this:

int myvar = 5;    // global

{
    int myvar = 6;    // local var shadows global
    int *pglob_myvar = (int *)dlsym(RTLD_NEXT, "myvar");
    printf("Local: %d, global: %d\n", myvar, *pglob_myvar);
}

If you want your code to look sexy, use macro:

#define GLOBAL_ADDR(a,b)  b =(typeof(b))dlsym(RTLD_NEXT, #a)
...
int *pglob_myvar;
GLOBAL_ADDR(myvar, pglob_myvar);
...
查看更多
Animai°情兽
3楼-- · 2020-01-23 04:23

what is a "shielded global variable" in pure C?

in C you have local variables, file local/global variables (static) and global variables (extern)

so file1.c:
int bla;

file2.c
extern int bla;
查看更多
仙女界的扛把子
4楼-- · 2020-01-23 04:33

There is no :: in c but you can use a getter function

#include <stdio.h>

int L=3;

inline int getL()
{
   return L;
}

int main();
{
   int L = 5;

   printf("%d, %d", L, getL());
}
查看更多
再贱就再见
5楼-- · 2020-01-23 04:37

Depending on what you call shielded global variable in C, different answers are possible.

If you mean a global variable defined in another source file or a linked library, you only have to declare it again with the extern prefix:

extern int aGlobalDefinedElsewhere;

If you mean a global variable shadowed (or eclipsed, choose the terminology you prefer) by a local variable of the same name), there is no builtin way to do this in C. So you have either not to do it or to work around it. Possible solutions are:

  • getter/setter functions for accessing global variable (which is a good practice, in particular in multithreaded situations)

  • aliases to globals by way of a pointer defined before the local variable:

    int noName;
    {
        int * aliasToNoName = &noName; /* reference to global */
        int noName;                    /* declaration of local */
        *aliasToNoName = noName;       /* assign local to global */
    }
    
查看更多
Deceive 欺骗
6楼-- · 2020-01-23 04:40

If your file-scope variable is not static, then you can use a declaration that uses extern in a nested scope:

int c;

int main() {
    {
        int c = 0;
        // now, c shadows ::c. just re-declare ::c in a 
        // nested scope:
        {
            extern int c;
            c = 1;
        }
        // outputs 0
        printf("%d\n", c);
    }
    // outputs 1
    printf("%d\n", c);
    return 0;
}

If the variable is declared with static, i don't see a way to refer to it.

查看更多
放荡不羁爱自由
7楼-- · 2020-01-23 04:43

Yet another option is to reference the global before defining your local, or at least get a pointer to it first so you can access it after defining your local.

#include <stdio.h>

int x = 1234;
int main()
{
   printf("%d\n",x); // prints global
   int x = 456;
   printf("%d\n",x); // prints local
}
查看更多
登录 后发表回答