简体   繁体   English

当函数返回一个结构时,它是保留在堆栈还是堆中?

[英]When function return an struct, it keeps at stack or at heap?

Sample: 样品:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct Animal{

    char name[100];
    char color[100];

} Animal;

Animal Animal_new() {

    Animal aux;
    return aux;
}

int main() {

    Animal a = Animal_new();    

}

The inner struct is created at stack 内部结构是在堆栈上创建的
The returning is a copy that is moved to the heap? 返回的是将副本移到堆吗?
The returning struct must be free? 返回的结构必须是免费的?

Functions returning a structure can be implemented by the compiler as taking an extra hidden argument, a pointer to the receiving struct in the caller's scope, possibly a temporary one if the result is not stored. 返回结构的函数可以由编译器实现为采用额外的隐藏参数,即调用方范围内的接收结构的指针,如果未存储结果,则可能是临时的。 The function copies its local copy to the destination one. 该函数将其本地副本复制到目标副本。 In your case, both are stored in automatic storage. 就您而言,两者都存储在自动存储中。

If your function had allocated memory from the heap for the structure it returns, the memory would likely become unreachable as the struct is returned by value (copied to the destination) and its address, known to the returning function will eventually be lost. 如果您的函数已从堆中为其返回的结构分配了内存,则该内存可能会变得无法访问,因为该struct将按值(复制到目标)返回,并且返回函数已知的其地址最终将丢失。

If the structure is small, the compiler can choose to return it in registers as it does for scalar values. 如果结构较小,则编译器可以选择将其返回到寄存器中,就像处理标量值一样。

The ABI defines how these matters are handled. ABI定义了这些事项的处理方式。 The ABI is system specific, it can be more of less complicated depending on hardware capabilities and implementer's choices. ABI是特定于系统的,根据硬件功能和实施者的选择,它可能变得更加简单。

struct s are returned by value. struct s按值返回。 In fact, the only way for anything to get onto the heap is for you to call malloc / calloc / realloc . 实际上,任何东西进入堆的唯一方法是调用malloc / calloc / realloc

The local value of Animal variable is going to be placed in automatic memory. Animal变量的本地值将被放置在自动存储器中。 When the value is returned to main , a copy is made from aux of Animal_new to a of main . 当值返回给main ,一个副本从由auxAnimal_newamain Compilers, however, can optimize things in a way that prevents copying, but one may not assume that this is true. 但是,编译器可以通过一种防止复制的方式来优化事物,但是人们可能并不认为这是真的。

The calling function allocated space on its stack for the return struct by the callee. 调用函数在其堆栈上为被调用者的返回结构分配了空间。

Basically since, in your example, main knows it's going to receive a struct Animal it automatically creates space on its stack for it and passes a pointer (this is hidden from you but happens at the assembly level) to this newly created stack space to your Animal_New() function. 基本上,因为在您的示例中,main知道它将要接收结构动物,所以它会自动为其在堆栈上创建空间,并将该新创建的堆栈空间的指针(这对您来说是隐藏的,但发生在组装级别)传递给您Animal_New()函数。

This function has its own copy of the struct in its stack space, when returning it copies its value to the caller stack space through the pointer it received. 该函数在其堆栈空间中具有自己的结构副本,返回时,它将通过其接收到的指针将其值复制到调用者堆栈空间中。

Asked by @MoLaiSkirulais in the comments: @MoLaiSkirulais在评论中询问:

Animal *heapAllocatedAnimal = malloc(sizeof(Animal));
memcpy(heapAllocatedAnimal,&a,sizeof(Animal));

As referenced by @Olaf this a "classical" implementation, which should represent most implementations however knowing is better than assuming so when in doubt read the documentation. 正如@Olaf所引用的那样,这是一个“经典”实现,应该代表大多数实现,但是,如果有疑问,那么阅读文档时要比假设更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM