如何在C中访问被跟踪的全局变量?

时间:2022-09-06 13:51:46

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

如何在C中访问被阴影的全局变量?在c++中,我可以使用::for全局命名空间。

7 个解决方案

#1


38  

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.

如果变量是用静态声明的,那么我没有办法引用它。

#2


21  

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

c中没有::,但是可以使用getter函数

#include <stdio.h>

int L=3;

inline int getL()
{
   return L;
}

int main();
{
   int L = 5;

   printf("%d, %d", L, getL());
}

#3


4  

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:

如果您正在讨论被跟踪的全局变量,那么(在Linux上)您可以使用dlsym()来查找全局变量的地址,如下所示:

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);
...

#4


2  

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

根据您在C中调用的屏蔽全局变量,不同的答案是可能的。

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前缀:

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:

如果您的意思是一个全局变量被相同名称的局部变量阴影(或重叠,请选择您喜欢的术语),那么在c中没有构建方法来实现这一点。可能的解决方案是:

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

    用于访问全局变量的getter/setter函数(这是一个很好的实践,特别是在多线程情况下)

  • 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 */
    }
    

#5


1  

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

纯C中的“屏蔽全局变量”是什么?

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

在C中,有本地变量、文件本地/全局变量(静态)和全局变量(extern)

so file1.c:
int bla;

file2.c
extern int bla;

#6


0  

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
}

#7


-1  

gyz y 2 think so much Just use pointer variable n store the address of the global variable in it n den use it inside d main(). using the pointer to refer the global variable will not be a problem even if a local variable with same name is there inside main().

gyzy 2想的太多了只需要使用指针变量n来存储全局变量的地址n den在d main()中使用它。使用指针来引用全局变量不会有问题,即使main()中有一个同名的局部变量。

#1


38  

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.

如果变量是用静态声明的,那么我没有办法引用它。

#2


21  

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

c中没有::,但是可以使用getter函数

#include <stdio.h>

int L=3;

inline int getL()
{
   return L;
}

int main();
{
   int L = 5;

   printf("%d, %d", L, getL());
}

#3


4  

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:

如果您正在讨论被跟踪的全局变量,那么(在Linux上)您可以使用dlsym()来查找全局变量的地址,如下所示:

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);
...

#4


2  

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

根据您在C中调用的屏蔽全局变量,不同的答案是可能的。

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前缀:

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:

如果您的意思是一个全局变量被相同名称的局部变量阴影(或重叠,请选择您喜欢的术语),那么在c中没有构建方法来实现这一点。可能的解决方案是:

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

    用于访问全局变量的getter/setter函数(这是一个很好的实践,特别是在多线程情况下)

  • 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 */
    }
    

#5


1  

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

纯C中的“屏蔽全局变量”是什么?

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

在C中,有本地变量、文件本地/全局变量(静态)和全局变量(extern)

so file1.c:
int bla;

file2.c
extern int bla;

#6


0  

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
}

#7


-1  

gyz y 2 think so much Just use pointer variable n store the address of the global variable in it n den use it inside d main(). using the pointer to refer the global variable will not be a problem even if a local variable with same name is there inside main().

gyzy 2想的太多了只需要使用指针变量n来存储全局变量的地址n den在d main()中使用它。使用指针来引用全局变量不会有问题,即使main()中有一个同名的局部变量。