刺激战场
六合彩
贵宾厅
  • 5343阅读
  • 1回复

Question2.9:How are structure passing and returning implemented? [复制链接]

上一主题 下一主题
离线那个谁
 

只看楼主 倒序阅读 使用道具 楼主  发表于: 2013-07-18
— 本帖被 tyrone2497谁 执行加亮操作(2013-07-19) —
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.
离线那个谁

只看该作者 沙发  发表于: 2013-07-18
问题:结构体的传递和返回是如何实现的?
回答:当结构体作为参数传递给函数的时候,整个结构体都是被压入栈中的,消耗所需要的字节数(程序员经常使用指向结构体的指针来避免这种情况)。一些编译器仅仅是传递一个结构体的指针,尽管它们不得不做一个局部副本来保存传递值的语义。
编译器会提供一些额外的“隐藏”的参数指向从函数返回的结构体。有些老式的编译器使用一个特殊的静态位置来返回结构体。这会导致返回结构体的函数不可再进入,而这是ANSIC所不允许的。
快速回复
限100 字节
如果您提交过一次失败了,可以用”恢复数据”来恢复帖子内容
 
上一个 下一个