Is it safe to make a comparison without storing the return value in the stack

Viewed 52

Here is the sample code:

#include <iostream>
#include <stdlib.h>
#include <cstdio>

int add(int x, int y){
    return x + y;
}

int main(){
    int x = 10;
    int y = 10;
    if(add(x, y) == 20){
        printf("Here I am\n");
    }

    return 0;
}

I make a comparison between constant value 20 and the returned value of add()(without saving the returned value in a local variable). I was wondering is it safe to do that in c++?

My argument is that if we don't store the returned value back in the stack frame of main(), the returned value literally doesn't exist in the stack and it is only stored in the register like %eax, what if the value stored in %eax is edited before the comparison is made, then the program would probably go wrong.

Below is part of the assembly of the code block: enter image description here

1 Answers

Yes, this is perfectly safe.

When you call a function, its return value becomes a temporary object in the caller's scope. Such temporary objects' lifetime extends to the end of the full expression in which they were created, which is the expression add(x, y) == 20 in this case. Thus it is guaranteed to live long enough for the comparison to be done.


As an aside, I would recommend not thinking about the stack and registers when writing C++. Those things don't exist as far as the C++ language standard is concerned. Instead think about object lifetimes, which are specified by the language. Even on a real implementation, a named variable doesn't necessarily mean the object will ever be stored in memory. The compiler decides where and how to store objects, and it will often store an object purely in registers if it can get away with it, or it may not store them at all if it knows it doesn't need them. Indeed, on any competent modern optimizing compiler, your entire program will likely be reduced to something like

mov edi, OFFSET_OF_STRING_CONSTANT
call printf
xor eax, eax
ret

Since the compiler knows the values of x and y and add is simple enough that it can determine the result of the comparison at compile time and avoid emitting any code for that call or comparison at all. Compiler Explorer example.

Related