如果在 C++ 中不返回值会发生什么? [英] What happens if you don't return a value in C++?

查看:24
本文介绍了如果在 C++ 中不返回值会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天,我发现自己在写这样的代码:

Yesterday, I found myself writing code like this:

SomeStruct getSomeStruct()
{
    SomeStruct input;

    cin >> input.x;
    cin >> input.y;
}

当然忘记实际返回我刚刚创建的结构.奇怪的是,这个函数返回的 结构中的值被初始化为零(当使用 g++ 编译时).这只是巧合,还是隐式地在某处创建并初始化了另一个 SomeStruct?

Of course forgetting to actually return the struct I just created. Oddly enough, the values in the struct that was returned by this function got initialized to zero (when compiled using g++ that is). Is this just a coincidence or did another SomeStruct get created and initialized somewhere implicitly?

推荐答案

是否在某处隐式创建并初始化了另一个 SomeStruct?

Did another SomeStruct get created and initialized somewhere implicitly?

想想结构是如何返回的.如果 xy 都是 32 位,则它太大而无法放入 32 位架构上的寄存器中,同样适用于 64 位值64 位架构(@Denton Gentry 的回答提到了如何返回更简单的值),所以它必须分配到某个地方.为此使用堆会很浪费,因此必须在堆栈上分配它.但它不能在 getSomeStruct 函数的堆栈帧上,因为在函数返回后它不再有效.

Think about how the struct is returned. If both x and y are 32 bits, it is too big to fit in a register on a 32-bit architecture, and the same applies to 64-bit values on a 64-bit architecture (@Denton Gentry's answer mentions how simpler values are returned), so it has to be allocated somewhere. It would be wasteful to use the heap for this, so it has to be allocated on the stack. But it cannot be on the stack frame of your getSomeStruct function, since that is not valid anymore after the function returns.

编译器通过向被调用函数传递一个指向分配给它的空间的隐藏指针,让调用者告诉被调用函数将结果放在哪里(可能在调用者的堆栈上的某个位置).所以,它被设置为零的地方是在 caller 上,而不是在你的 getSomeStruct 函数上.

The compiler instead has the caller tells the called function where to put the result (which is probably somewhere on the stack of the caller), by passing the called function a hidden pointer to the space allocated to it. So, the place where it is being set to zero is on the caller, not on your getSomeStruct function.

还有一些优化,比如命名值返回优化",可以省略额外的副本.因此,如果您使用了缺少的 return,结果将直接在调用者分配的空间上创建,而不是创建一个临时的并复制它.

There are also optimizations like the "named value return optimization" where extra copies can be elided. So, had you used the missing return, the result would be created directly on the space allocated by the caller, instead of creating a temporary and copying it.

要了解更多关于正在发生的事情,您必须查看调用者函数.它是否正在初始化(为零)一个空" SomeStruct 您稍后将 getSomeStruct 函数的返回值分配给它?还是在做其他事情?

To know more about what is happening, you have to look at the caller function. Is it initializing (to zero) an "empty" SomeStruct to which you later assign the return value of your getSomeStruct function? Or is it doing something else?

这篇关于如果在 C++ 中不返回值会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆