打印功能的地址 [英] printing the address of a function

查看:60
本文介绍了打印功能的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i我试图打印一个函数的地址而没有得到

编译器警告(我正在使用带有很多标志的gcc进行编译)。


如果我试试这个:


printf("%p",f);


i get:


警告:格式%p期望类型''void *;但是参数2的类型为''void

(*)void)''


如果我试试这个:


printf("%p",(void *)f);


i get:


ISO C禁止转换函数指针对象指针类型


如果我这样做:


printf("%x",((unsigned int)f));


它编译干净,但是这个解决方案让我感到烦恼,因为它假定

unsigned int与指针的长度相同。


关于正确的任何建议打印函数地址的方法?


谢谢,

rCs


i am trying to print the address of a function without getting a
compiler warning (i am compiling with gcc with alot of flags).

if i try this:

printf("%p", f);

i get:

warning: format %p expects type ''void *; but argument 2 has type ''void
(*) void)''

if i try this:

printf("%p", (void *)f);

i get:

ISO C forbids conversion of function pointer to object pointer type

if i try this:

printf("%x", ((unsigned int)f));

it compiles cleanly, but this solution bugs me because it assumes that
an unsigned int is the same length as a pointer.

any suggestions as to the "correct" way to print the address of a function?

thanks,
rCs

推荐答案

Robert Seacord写道:
Robert Seacord wrote:

i我试图打印函数的地址
i am trying to print the address of a function



这是不可移植的。正如您的消息(包括编译器的

警告)几乎指出的那样,

函数指针没有* printf格式说明符,并且没有保证整数类型要足够多了b $ b足以容纳它。 (这包括intptr_t和uintptr_t。)你为什么试图这样做?
?根据您的需要,您可以将地址存储在变量中,并将该变量作为
unsigned char数组访问,打印每个元素。 />

void(* fp)(void);

/ * ... * /

for(unsigned char * c =( unsigned char *)& fp; c!=(unsigned char *)

(& fp + 1); c ++)

printf("%x" ,(未签名)* c);

This cannot be done portably. As your message (including the compiler''s
warnings) almost pointed out, there is no *printf format specifier for
function pointers, and there is no integer type guaranteed to be large
enough to hold it. (This includes intptr_t and uintptr_t.) Why are you
trying to do this? Depending on your needs, you may be able to store
the address in a variable, and access that variable as an array of
unsigned char, printing each element.

void (*fp)(void);
/* ... */
for (unsigned char *c = (unsigned char *) &fp; c != (unsigned char *)
(&fp + 1); c++)
printf("%x ", (unsigned) *c);


Robert Seacord写道:
Robert Seacord wrote:

>

i我试图打印函数的地址而没有得到

编译器警告(我用gcc编译了很多标志)。
>
i am trying to print the address of a function without getting a
compiler warning (i am compiling with gcc with alot of flags).



.... snip ...

.... snip ...


>

关于正确的任何建议打印

函数地址的方法?
>
any suggestions as to the "correct" way to print the address of a
function?



你不能,而且仍然是便携式的。什么都没告诉你函数指针的大小

。例如,如果该

函数的代码位于共享库中,并且指针必须指定

文件,文件中的偏移量和文件的大小,该怎么办?实际代码,而不是
提到如何将该功能链接到任何附属功能。任何

尝试使用unsigned char指针来访问函数

指针将不知道哪些位是密切相关的。


什么你想做什么?


-

一些信息链接:

< news:news.announce.newusers

< http://www.geocities.com/nnqweb/>

< http://www.catb.org/~esr/faqs/smart- questions.html>

< http://www.caliburn.nl/topposting.html>

< http://www.netmeister.org/news/ learn2quote.html>

< http://cfaj.freeshell.org/google/>

You can''t, and still be portable. Nothing even tells you the size
of a function pointer. For example, what if the code for that
function is in a shared library, and the pointer has to specify the
file, offset within the file, and size of the actual code, not to
mention how to link that function to any subsidiary functions. Any
attempt to use an unsigned char pointer to access the function
pointer will not know what bits are germane.

What are you trying to do?

--
Some informative links:
<news:news.announce.newusers
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>


Robert Seacord发布:
Robert Seacord posted:

ISO C禁止将函数指针转换为对象指针类型
ISO C forbids conversion of function pointer to object pointer type



这是我听说过这个限制的第一个。 C中最小的可寻址

内存单位是字节(回避位域的技术性等等... b $等等......)。四个独立的内置指针类型保证准确地存储一个字节的地址:


char *

char签名*

char unsigned *

void *


(及其所有const / volatile / restrict变体。)


基于这些理由,我不明白为什么我们不能简单地做b $ b的逻辑:


void * p =(void *)SomeFunc;


当然,标准禁止它......但是物理定律不能保证它的成功b
成功......?


-


Frederick Gotham


This is the first I have heard of this restriction. The smallest addressable
unit of memory in C is the byte (sidestepping the technicalities of bitfields
and so forth...). Four separate built-in pointer types are guaranteed to
store accurately the address of a byte:

char*
char signed*
char unsigned*
void*

(and all their const/volatile/restrict variants.)

On these grounds, I don''t understand the logic pertaining to why we can''t
simply do:

void *p = (void*)SomeFunc;

Sure, the Standard forbids it... but do the laws of physics not guarantee its
success... ?

--

Frederick Gotham


这篇关于打印功能的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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