关于功能 [英] about functions

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

问题描述

采取以下功能:


char f(){

return(-1);

}


f是函数指针,我知道它是如何用来调用函数的。


但是,指针究竟指向什么?

存储的返回值在哪里?

是否可以在内存中获取整个函数的代码段地址,数据段地址或

大小?


谢谢!

take the function:

char f(){
return(-1);
}

f is the function pointer, i know how it is used to call a function.

But, what exactly does the pointer point to?
Where is the return value stored?
Is it possible to get the code segment address, data segment address or the
size of the whole function in memory?

Thanks!

推荐答案

" Martin Johansen" <毫安****** @ is.online.no>写道:
"Martin Johansen" <ma******@is.online.no> writes:
取功能:

char f(){
return(-1);
}

f是函数指针,我知道它是如何用来调用函数的。

但是,指针究竟指向什么?


它以某种未指明的方式指向该功能,即C标准

未指定它如何指向该功能。

存储的返回值在哪里?


这取决于实施情况。 C标准没有规定如何返回返回值

是否可以获取代码段地址,数据段地址
或者大小内存中的整个功能?
take the function:

char f(){
return(-1);
}

f is the function pointer, i know how it is used to call a function.

But, what exactly does the pointer point to?
It points to the function, in some unspecified way, i.e. the C standard
does not specify how it points to the function.
Where is the return value stored?
That depends on the implementation. The C standard does not specify how
the return value is returned.
Is it possible to get the code segment address, data segment address
or the size of the whole function in memory?




不,不在标准C中。代码段和/或数据段甚至不需要存在



如果您对

特定系统中的这些内容感兴趣,请在专用于该系统的小组中询问。


马丁

-

, - 。 Martin Dickopp,德国德累斯顿,=, - _-。 =。

/, - ) http://www.zero -based.org/ ((_ /)oo(\_))

\` - ''` - ''(。)` - ''

` - 。 Debian,GNU操作系统的一种变体。 \_ /



No, not in standard C. A code segment and/or data segment need not even
exist.

If you are interested in how these things are implemented on your
particular system, please ask in group dedicated to that system.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-'' `-''(. .)`-''
`-. Debian, a variant of the GNU operating system. \_/


在< qX ***************** @ news4.e.nsc.no> 马丁约翰森 <毫安****** @ is.online.no>写道:
In <qX*****************@news4.e.nsc.no> "Martin Johansen" <ma******@is.online.no> writes:
取功能:

char f(){
return(-1);
}

f是函数指针,我知道它是如何用来调用函数的。

但是,指针究竟指向什么?


它提供了调用该函数所需的信息。当

这个信息不适合指针时,它指向一个地方

包含这个信息。例如,在IA64架构上,为此目的需要两个
64位地址,函数指针

因此,需要双重间接取消引用该平台的

上的函数指针。

存储的返回值在哪里?


这与函数指针完全无关。这些天返回值通常存储在寄存器中。

是否可以获取代码段地址,数据段地址或整个
大小函数在内存中?
take the function:

char f(){
return(-1);
}

f is the function pointer, i know how it is used to call a function.

But, what exactly does the pointer point to?
It provides the necessary information for calling that function. When
this information doesn''t fit into a pointer, it points to a place
containig this information. For example, on the IA64 architecture, two
64-bit addresses are needed for this purpose and a function pointer
actually points to a function descriptor that contains these addresses.
So, it takes a double indirection to dereference a function pointer on
that platform.
Where is the return value stored?
That''s entirely irrelevant to function pointers. Return values are
typically stored in registers, these days.
Is it possible to get the code segment address, data segment address or the
size of the whole function in memory?




在常见的实现中,函数指针在文本段中存储函数的条目

地址。功能本身知道它必须操纵的对象的地址

。但是,这个简单而通用的模型还有例外情况,AS / 400甚至比IA64更具异国情调:

函数指针是768位(左右)实体这个平台。


Dan

-

Dan Pop

DESY Zeuthen,RZ group

电子邮件: Da*****@ifh.de


Martin Johansen写道:
Martin Johansen wrote:
取功能:

char f(){
return(-1);
}

f是函数指针,我知道它是如何用来调用函数的。


正如其他人所说,f不是函数指针。 " F"表示函数的

位置。编译器可能根本不使用任何指向

函数的指针。取决于实施。可以想象,将函数的地址压入堆栈,然后弹出。进入

程序计数器(或ip - 指令指针)。


但是,指针究竟指向什么?


函数指针是:

char(* Pointer_To_f)();

上面的行声明了一个指向任何类型的指针功能

与你的''f'功能具有相同的签名。


可以选择f功能的位置和把它放进

a指针:

typedef char(* Pointer_To_f)();

Pointer_To_f p_f;

p_f = f;

存储的返回值在哪里?
不必存储返回值。

编译器可以优化此函数,只需将

调用变量赋值为-1。 br />

有些实现让您可以随心所欲地随时随地放置
数据。有时,常量数据与可执行文件位于同一区域,有时候不是b $ b。取决于编译器和实现。


是否可以在内存中获取代码段地址,数据段地址或整个函数的大小?
我已经问过这个小组,如果只用便携式C可以确定一个函数的大小

,但是他们说b / b
表示没有。必须使用其他平台相关技巧

来确定函数的大小。


就起始位置而言,是的,它可以获得

使用C.但是,C语言没有代码概念

段或数据段。这是一个实施问题。

谢谢!
take the function:

char f(){
return(-1);
}

f is the function pointer, i know how it is used to call a function.
As others have stated, f is not the function pointer. "f" reprents the
location of a function. The compiler may not use any pointers to the
function at all. Depends on the implementation. It is conceivable that
the address of the function is pushed on the stack, then "popped" into
the program counter (or ip -- instruction pointer).


But, what exactly does the pointer point to?
A function pointer is:
char (*Pointer_To_f)();
The above line declares a pointer to any kind of function that
has the same signature as your ''f'' function.

One can take the location of the ''f'' function and place it into
a pointer:
typedef char (*Pointer_To_f)();
Pointer_To_f p_f;
p_f = f;
Where is the return value stored? The return value does not have to be stored.
A compiler could optimize this function and just assign the
calling variable to "-1".

Some implementations give you the freedom to place constant
data wherever you want to. Sometimes the constant data is
placed in the same region that the executable is, sometimes
not. Depends on the compiler and the implementation.

Is it possible to get the code segment address, data segment address or the
size of the whole function in memory? I''ve already asked this group if the size of a function
could be determined using only portable C, but they
said no. Other platform dependent tricks must be used
to determine the size of a function.

As far as the starting location, yes it can be obtained
using C. However, the C language has no concepts of code
segments or data segments. That is an implementation issue.

Thanks!



-

Thomas Matthews


C ++新闻组欢迎信息:
http: //www.slack.net/~shiva/welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


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

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