免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: yazi0127
打印 上一主题 下一主题

[C] 请教:C语言函数的返回值类型能否为结构体? [复制链接]

论坛徽章:
0
11 [报告]
发表于 2010-01-26 23:47 |显示全部楼层
回复 #23 OwnWaterloo 的帖子

以你的结构,结果:

[root@Server01 struct]# cat test2.c
typedef struct {
  int x;
  int y;
} Point2D32S;

Point2D32S point2d32s(int x,int y) {
  Point2D32S result = {x,y};
  return result;
}

typedef struct {
  short x;
  short y;
} Point2D16S;

Point2D16S point2d16s(short x,short y) {
  Point2D16S result = {x,y};
  return result;
}

typedef struct {
  int val;
} atomic_t;

atomic_t atomic(int val) {
  atomic_t result = {val};
  return result;
}

int main()
{
  Point2D32S  a ;

  a = point2d32s(0x55 , 0xAA) ;

  return a.x > a.y ? 1 : 0 ;
  
}
[root@Server01 struct]# gcc -Wall -O2 -fomit-frame-pointer -S test2.c
[root@Server01 struct]#ls *.s
test2.s
[root@Server01 struct]# cat test2.s
        .file        "test2.c"
        .text
        .p2align 4,,15
.globl point2d32s
        .type        point2d32s, @function
point2d32s:
        movl        4(%esp), %eax
        movl        12(%esp), %edx
        movl        %edx, 4(%eax)
        movl        8(%esp), %edx
        movl        %edx, (%eax)
        ret        $4
        .size        point2d32s, .-point2d32s
        .p2align 4,,15
.globl point2d16s
        .type        point2d16s, @function
point2d16s:
        movl        4(%esp), %eax
        movl        12(%esp), %edx
        movw        %dx, 2(%eax)
        movl        8(%esp), %edx
        movw        %dx, (%eax)
        ret        $4
        .size        point2d16s, .-point2d16s
        .p2align 4,,15
.globl atomic
        .type        atomic, @function
atomic:
        movl        4(%esp), %eax
        movl        8(%esp), %edx
        movl        %edx, (%eax)
        ret        $4
        .size        atomic, .-atomic
        .p2align 4,,15
.globl main
        .type        main, @function
main:
        leal        4(%esp), %ecx
        andl        $-16, %esp
        pushl        -4(%ecx)
        pushl        %ecx
        subl        $20, %esp
        leal        12(%esp), %eax
        movl        %eax, (%esp)
        movl        $170, 8(%esp)
        movl        $85, 4(%esp)
        call        point2d32s
        subl        $4, %esp
        movl        12(%esp), %eax
        movl        16(%esp), %edx
        cmpl        %eax, %edx
        setl        %al
        addl        $20, %esp
        popl        %ecx
        movzbl        %al, %eax
        leal        -4(%ecx), %esp
        ret
        .size        main, .-main
        .ident        "GCC: (GNU) 4.1.1 20070105 (Red Hat 4.1.1-52)"
        .section        .note.GNU-stack,"",@progbits
[root@Server01 struct]#

论坛徽章:
0
12 [报告]
发表于 2010-01-27 00:12 |显示全部楼层
原帖由 OwnWaterloo 于 2010-1-26 23:59 发表
"以我的代码"并不能证明什么。

看29楼, 只要能够举出一个没有采用隐式指针参数的反例, 就可以说明这个结论是错误的:



结论是 : 并不都是这样。

上面5份代码, 全都是通过寄存器返回的, 没有写 ...



gcc你我编译的结果相差甚远 . 我编译的支持我的观点.

相信你的VC编译结果 : VC上小结构 , 可以不用隐式指针传递.

也就是如何处理与编译器有关.

论坛徽章:
0
13 [报告]
发表于 2010-01-27 00:22 |显示全部楼层
原帖由 w_anthony 于 2010-1-27 00:09 发表


我想我是得再清楚一点,这里的例子举的不好,struct A太小了,结果就是OwnWaterloo,直接用寄存器了,看不出你所说的“拷贝无法自圆其说论”,改成
struct A
{
     int i, j, g;
};
就行了。


你这想说明什么?!

论坛徽章:
0
14 [报告]
发表于 2010-01-27 01:20 |显示全部楼层

回复 #36 w_anthony 的帖子

目前的结论是,在对待小结构的处理上,各编译器有所不同 ; 大结构用隐式指针传递 .
只要需要 , 完全可以大胆直接返回结构.

至于你说的
void func(struct A* p)
{
    struct A a;
    a.i = 0;
    *p = a;        //拷贝发生在这里

}

即便不是"漏洞百出",也一样以偏盖全了.

论坛徽章:
0
15 [报告]
发表于 2010-01-27 01:23 |显示全部楼层
原帖由 w_anthony 于 2010-1-26 23:31 发表
最烦那些一上来就装X的,摆出一付自己高人一等架势的家伙。就算你再牛X,谦虚一点不行么?更何况凡是人,都有出错的可能,给别人点面子,也给自己留条后路。

struct A
{
    int i;
...


我还是澄清一下:

发送到:         老手
时间:         2010-1-26 23:57
内容:        
原始短消息: 求教

QUOTE:
原始短消息: 求教



函数返回和a=b再具体的处理上是不一样的.
你还是看帖子吧.

言重之处,请多包涵! 都是来交流的嘛,只要不是人身"母鸡"就好.

这个没事,正好以前理解有误,多谢纠正

论坛徽章:
0
16 [报告]
发表于 2010-01-28 16:03 |显示全部楼层
原帖由 daviescai 于 2010-1-28 15:13 发表
ABI


是的. 可能就是二进制兼容的需要导致了gcc在windows和linux上不同的编译结果 .
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP