从函数外部访问静态变量 [英] accessing static variables from outside their function

查看:100
本文介绍了从函数外部访问静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道在函数外部函数中访问和修改声明

static的变量的方法?请不要

homilies为什么这是不好的做法:上下文非常特殊和

涉及自动生成的代码。我知道其他几种方法可以攻击我的问题,但如果可以的话,这将是最干净的工作。


多一点背景。我使用C作为生成

系统的代码的输出,它可以并经常输出很多具有

一致声明集的函数,包括变量名,在顶部。

其中一个通常是一个静态整数,称之为完成,如果已设置为1,则会立即返回
,否则功能

继续。我认为这是一个众所周知,琐碎和平庸的作品

代码,允许函数只运行一次。


然而,现在,出现这样的情况:

能够从声明它的b / b
函数外部重置此标志。像我一样使用gcc和-rdynamic编译,我经常使用dlopen和dlsym来查找函数的地址。

我有(天真地)想象dladdr()会允许我在函数中获取地址声明为静态的符号
,但据我所知

dladdr()只访问全局符号。所以我要找的是一个

方法,用于查找静态整数变量的地址,名为

" done"在function001中声明,而不是所有其他名为done的静态

整数变量。在所有其他类似的函数中声明,以这种方式我可以改变它的价值 - 所以

只是得到副本不会做。


我在这个组中询问而不是在gcc特定的地方,因为在

原则这不是gcc特定的问题而且我的软件编译

使用具有可比性技巧的其他编译器。更通用的和/ b $ b手术解决方案越好。但是我的函数可能有一个变量和

不可预测的参数数量。


指针,带页面引用的RTFM和哲学思考所有

欢迎。但是一个说明性的代码片段将是完美的。


-

Steve Blinkhorn< st *** @ prd.co.uk>

解决方案

Steve Blinkhorn说:

有没有人知道一种访问和修改变量的方法
static在函数外部的函数内?




Eeeeewwww。 :-)


一个显而易见的方法 - 你可以破解一个文件范围指针,并在第一次将它初始化为
,指向静态对象。

int * flagptr;


void foo(void)/ *我猜你的界面是用石头写的! * /

{

static int flag = 0; / *这是兔子* /


if(flagptr == NULL)

{

flagptr =& flag;

}


/ *不管你的船漂浮...... * /


返回;

}


/ *一个不同的源文件* /


extern int * flagptr;


无效栏(无效)

{

foo();

* flagptr = 1; / * hack,chop,maim * /

foo();

}

-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


Steve Blinkhorn写道:
< blockquote class =post_quotes>有没有人知道在函数外部函数中访问和修改声明静态变量的方法?
...
多一点上下文。我使用C作为代码生成系统的输出,它可以并经常输出很多函数,这些函数在顶部具有一致的声明集,包括变量名。
其中一个通常是一个静态整数,称之为完成,如果已经设置为1则立即返回,否则函数继续进行。我认为这是一个众所周知的,琐碎的,平庸的代码,允许函数只运行一次。

然而,现在已经出现了可能需要它的情况
能够从声明它的函数外部重置此标志。

我在这个组中而不是在gcc特定的地方要求因为在不可预测的参数。

指针,带有页面引用的RTFM和哲学思考都欢迎。但是一个说明性的代码片段将是完美的。




两个想法:


(a)(编译器特定)检查来源一个调试器使用你的工具链工作

,看看如何将变量名映射到它的运行

时间地址。


(b)更改问题定义。写一个过滤器(awk,perl等)到

转换这个:


(文件xxx.c)


... yyy(...)

{

静态完成;

/ *剩下的就在这里* / < br $>
}


到这个:


int xxx_yyy_done;


... yyy(...)

{

/ *剩下的就在这里* /

}

(b和1/2)修改代码生成系统。直接制作

表格(b)


在文章< 12 *********** **@corp.supernews.com>,

Steve Blinkhorn< st *** @ newsole.prd.co.uk>写道:

有没有人知道从函数外部访问和修改声明
静态函数的方法?
但是,现在出现了这样的情况,即可能希望能够从声明它的函数外部重置此标志。正如我所做的那样,使用gcc和-rdynamic进行编译,我常常能够使用dlopen和dlsym来查找函数的地址。
我在这个组中询问而不是在gcc特定的地方,因为在
原则中这不是特定于gcc的问题,而且我的软件使用具有类似技巧的其他编译器编译



不,原则上它*是* gcc特定的 - 或者至少是OS特定的,

如果操作系统规定所有可执行文件必须使用相同的调试器

表格格式。


在某些系统上,gcc会发出调用表条目,这些条目在未知的可执行部分中是
OS提供的调试器(因此系统调试器会忽略
。)


C标准中没有要求任何符号表

必须保留,除了讨论所隐含的内容之外

的外部链接。 (这并不意味着链接器必须存在
:编译器要求在同一命令行上命名所有源

是有效的,并且编译器可以在内存中执行所有操作

。)

除了可能存在不同的可执行格式之外的任何其他内容。

不被视为comp.lang.c的主题

-

法律 - 它是商品

- Andrew Ryan(环球邮报,2005/11/26)


Does anyone know of a way of accessing and modifying variables declared
static within a function from outside that function? Please no
homilies on why it''s bad practice: the context is very particular and
involves automatically generated code. I know several other ways of
attacking my problem, but this would be the cleanest if it could be made
to work.

A little more context. I use C as the output of a code generating
system which can, and frequently does, output very many functions that have a
consistent set of declarations, including variable names, at the top.
One of these is typically a static integer, call it "done", which causes
an immediate return if it has been set to 1, otherwise the function
proceeds. I take it that this is a well-known, trivial and banal piece
of code for permitting a function to run just once.

Now, however, circumstances have arisen where it might be desirable to
be able to reset this flag from outside the function in which it is
declared. Compiling, as I do, using gcc with -rdynamic, I am routinely
able, using dlopen and dlsym, to find the address of a function by name.
I had (naively) imagined that dladdr() would allow me to get the addresses
of symbols declared static within functions, but as I understand it
dladdr() only accesses global symbols. So what I am looking for is a
method for finding the address of a static integer variable called
"done" declared within "function001", as opposed to all other static
integer variables called "done" declared within all the many other
similar functions, in such a way that I can change its value - so
just getting a copy won''t do.

I am asking in this group rather than in a gcc specific place because in
principle this is not a gcc-specific issue and my software compiles
using other compilers with comparable tricks. The more generic and
surgical a solution the better. But my functions may have a variable and
unpredictable number of arguments.

Pointers, RTFMs with page references and philosophical musings all
welcome. But an illustrative code fragment would be perfect.

--
Steve Blinkhorn <st***@prd.co.uk>

解决方案

Steve Blinkhorn said:

Does anyone know of a way of accessing and modifying variables declared
static within a function from outside that function?



Eeeeewwww. :-)

One obvious way - you could hack up a file scope pointer, and initialise it
first time in, to point at the static object.
int *flagptr;

void foo(void) /* I''m guessing your interface is written in stone! */
{
static int flag = 0; /* this is the bunny */

if(flagptr == NULL)
{
flagptr = &flag;
}

/* whatever floats your boat... */

return;
}

/* a different source file */

extern int *flagptr;

void bar(void)
{
foo();
*flagptr = 1; /* hack, chop, maim */
foo();
}
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


Steve Blinkhorn wrote:

Does anyone know of a way of accessing and modifying variables declared
static within a function from outside that function?
...
A little more context. I use C as the output of a code generating
system which can, and frequently does, output very many functions that have a
consistent set of declarations, including variable names, at the top.
One of these is typically a static integer, call it "done", which causes
an immediate return if it has been set to 1, otherwise the function
proceeds. I take it that this is a well-known, trivial and banal piece
of code for permitting a function to run just once.

Now, however, circumstances have arisen where it might be desirable to
be able to reset this flag from outside the function in which it is
declared.

I am asking in this group rather than in a gcc specific place because in
principle this is not a gcc-specific issue and my software compiles
using other compilers with comparable tricks. The more generic and
surgical a solution the better. But my functions may have a variable and
unpredictable number of arguments.

Pointers, RTFMs with page references and philosophical musings all
welcome. But an illustrative code fragment would be perfect.



Two ideas:

(a) (Compiler specific) Check the sources of a debugger that works
with your toolchain, to see how to map a variable name into its run
time address.

(b) Change the problem definition. Write a filter (awk, perl, etc.) to
convert this:

(file xxx.c)

... yyy(...)
{
static int done;
/* the rest goes here */
}

into this:

int xxx_yyy_done;

... yyy(...)
{
/* the rest goes here */
}

(b and 1/2) Modify the "code generating system" to directly produce
form (b)


In article <12*************@corp.supernews.com>,
Steve Blinkhorn <st***@newsole.prd.co.uk> wrote:

Does anyone know of a way of accessing and modifying variables declared
static within a function from outside that function? Now, however, circumstances have arisen where it might be desirable to
be able to reset this flag from outside the function in which it is
declared. Compiling, as I do, using gcc with -rdynamic, I am routinely
able, using dlopen and dlsym, to find the address of a function by name. I am asking in this group rather than in a gcc specific place because in
principle this is not a gcc-specific issue and my software compiles
using other compilers with comparable tricks.



No, in principle it *is* gcc-specific -- or at least OS specific,
if the OS dictates that all executables must use the same debugger
table format.

On some systems, gcc emits debugging table entries that are
in executable sections not known to the OS-supplied debugger (and hence
are ignored by the system debugger.)

There is no requirement in the C standard that a symbol table of any
sort must be kept, beyond that which is implied by the discussions
of "external linkage" (which does not imply that a "linker" must
exist: it would be valid for a compiler to require that all sources
be named on the same command line, and for the compiler to do everything
in-memory.)

Anything further than "different executable formats may exist" is
not considered to be on topic for comp.lang.c
--
"law -- it''s a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)


这篇关于从函数外部访问静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆