免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1934 | 回复: 0
打印 上一主题 下一主题

翻旧贴,关于函数返回结构等等 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-06 15:48 |只看该作者 |倒序浏览
偶尔翻到前面的旧贴,讨论函数返回的问题,看起来那么多回复吧,好多都乱乱的不知道在说些什么,我就纳闷了,为什么有些人不太明白也不去求证一下,就我猜我猜我猜猜呢,结果简单的问题变复杂了,跑题的也不鲜见...

好,牢骚结束
问题是关于函数返回值,也不一定是返回结构了,按照gcc internal来分,是分成scalar return和aggregate return,见gcc internals:http://gcc.gnu.org/onlinedocs/gc ... l#Stack-and-Calling

scalar return:通常情况,不用多说了
http://gcc.gnu.org/onlinedocs/gc ... .html#Scalar-Return
17.10.8 How Scalar Function Values Are Returned
This section discusses the macros that control returning scalars as values—values that can fit in registers. ......

aggregate return:返回复合数据结构,caller要传递返回数据存放的地址给callee
http://gcc.gnu.org/onlinedocs/gccint/Aggregate-Return.html
17.10.9 How Large Values Are Returned
When a function value's mode is BLKmode (and in some other cases), the value is not returned according to TARGET_FUNCTION_VALUE (see Scalar Return). Instead, the caller passes the address of a block of memory in which the value should be stored. This address is called the structure value address.

就是这样子,而且按照我的理解和实验来看,也没有什么memcpy拷贝来拷贝去的情况

稍微再复杂化一点,gcc对于后者也就是aggregate return又做了处理,提供了两个编译选项,默认的-fpcc-struct-return就是按照前面的说法做的,-freg-struct-return则是尽量用寄存器来存放返回值,效率可能高一点点,但是可能存在与其他编译器不兼容的情况。选项的解释如下:
-fpcc-struct-return
    Return “short” struct and union values in memory like longer ones, rather than in registers. This convention is less efficient, but it has the advantage of allowing intercallability between GCC-compiled files and files compiled with other compilers, particularly the Portable C Compiler (pcc).
    The precise convention for returning structures in memory depends on the target configuration macros.
    Short structures and unions are those whose size and alignment match that of some integer type.
    Warning: code compiled with the -fpcc-struct-return switch is not binary compatible with code compiled with the -freg-struct-return switch. Use it to conform to a non-default application binary interface.

-freg-struct-return
    Return struct and union values in registers when possible. This is more efficient for small structures than -fpcc-struct-return.
    If you specify neither -fpcc-struct-return nor -freg-struct-return, GCC defaults to whichever convention is standard for the target. If there is no standard convention, GCC defaults to -fpcc-struct-return, except on targets where GCC is the principal compiler. In those cases, we can choose the standard, and we chose the more efficient register return alternative.
    Warning: code compiled with the -freg-struct-return switch is not binary compatible with code compiled with the -fpcc-struct-return switch. Use it to conform to a non-default application binary interface.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP