找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 265|回复: 1

Question2.9:How are structure passing and returning implemented?

[复制链接]

210

主题

371

回帖

0

积分

管理员

积分
0
发表于 2013-7-18 20:14:15 | 显示全部楼层 |阅读模式
Q: How are structure passing and returning implemented?

A: When structures are passed as arguments to functions, the entire structure is typically pushed on the stack, using as many words as are required. (Programmers often choose to use pointers to structures instead, precisely to avoid this overhead.) Some compilers merely pass a pointer to the structure, though they may have to make a local copy to preserve pass-by-value semantics.
Structures are often returned from functions in a location pointed to by an extra, compiler-supplied ``hidden'' argument to the function. Some older compilers used a special, static location for structure returns, although this made structure-valued functions non-reentrant, which ANSI C disallows.

210

主题

371

回帖

0

积分

管理员

积分
0
 楼主| 发表于 2013-7-18 20:42:36 | 显示全部楼层
问题:结构体的传递和返回是如何实现的?
回答:当结构体作为参数传递给函数的时候,整个结构体都是被压入栈中的,消耗所需要的字节数(程序员经常使用指向结构体的指针来避免这种情况)。一些编译器仅仅是传递一个结构体的指针,尽管它们不得不做一个局部副本来保存传递值的语义。
编译器会提供一些额外的“隐藏”的参数指向从函数返回的结构体。有些老式的编译器使用一个特殊的静态位置来返回结构体。这会导致返回结构体的函数不可再进入,而这是ANSIC所不允许的。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

果子博客
扫码关注微信公众号

Archiver|手机版|小黑屋|风叶林

GMT+8, 2026-2-2 04:22 , Processed in 0.097820 second(s), 20 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表