How does function ACTUALLY return struct variable in C?

前端 未结 3 1101
我寻月下人不归
我寻月下人不归 2020-12-02 12:38

How does a function return value is clear to me, just to kick start:

int f()
{
  int a = 2;
  return a;
}

Now a gets the memor

3条回答
  •  没有蜡笔的小新
    2020-12-02 12:57

    A struct, at least a large one, will be allocated and returned on the stack, and will be popped off the stack (if at all) by the caller. The compiler will try to allocate it in the same spot where the caller is expecting to find this, but it will make a copy if that is not possible. It is possible, but not necessary that there is also a pointer to the struct, returned via registers.

    Of course the details will vary depending on architecture.

提交回复
热议问题