简体   繁体   English

C ++对返回但不存储的值会发生什么?

[英]C++ what happens to a value that is returned but is not stored?

Say I have a function that returns an int . 说我有一个返回int的函数。 I don't store the value from the function call. 我不存储函数调用中的值。 I presume that it is not stored in memory and fades into the aether, but I don't know. 我认为它没有存储在内存中并逐渐消失在以太中,但是我不知道。 Thank you. 谢谢。

An int return value will normally be stored in a register (eg, EAX or RAX on 32-bit or 64-bit Intel, respectively). int返回值通常将存储在寄存器中(例如,分别在32位或64位Intel上为EAX或RAX)。

It won't fade. 它不会褪色。 It'll simply be overwritten when the compiler needs that register for some other purpose. 当编译器出于其他目的而需要注册时,它将被简单地覆盖。 If the function in question is expanded inline, the compiler may detect that the value isn't used, and elide the code to write or compute the value at all. 如果有问题的函数被内联扩展,则编译器可能会检测到未使用该值,并退出代码以编写或计算该值。

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

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