1

I know that inline functions get copied into the code each time they are referenced. From my current understanding, I think it would be ok to return the address of an rvalue from an inline function since the function is inside the rest of the code and therefore the rvalue belongs to the scope of the caller function. Is this true?

example:

inline int* addressOfNum(int num){
    return #
} 

int main(){
    int* mynum = addressOfNum(9);
    
    // is the answer "11" or "segmentation fault"?
    int eleven = *mynum + 2;
}
  • No, you will have a dangling pointer. Also, note the expression `num` is an lvalue. If you use that dangling pointer you'll have undefined behavior. – Jason Jun 21 '22 at 12:11
  • You cannot take the address of an r-value. – paolo Jun 21 '22 at 12:12
  • 2
    No, that's not true. The scope is determined by the source text, not by the compiler's ultimate output. The validity of a piece of code can't depend on whether the compiler decides to inline a function call or not. (Also, `inline` does not mean what you think it means, and `num` is not an rvalue.) – molbdnilo Jun 21 '22 at 12:12
  • 2
    you can think of an inlined function call as being replaced literally at the place where it is called, but this mental image needs `{}` around the function body to be correct, ie it changes nothing about scope. – 463035818_is_not_an_ai Jun 21 '22 at 12:17
  • *"I know that inline functions get copied into the code each time they are referenced."* This is false. `inline` keyword was always only a hint to the compiler, that could be completely ignored, and most compilers for the past 15 years completely ignored that hint, because they optimize better than you can. Still, even if it wasn't ignored, compiler must produce code that has the same effects as what you wrote (unless UB exists). – Yksisarvinen Jun 21 '22 at 12:18
  • 3
    due to historical reasons `inline` is rather confusing. The keyword `inline` only means: The function can be defined in a header. It has no influence on whether the compiler will inline calls to the function – 463035818_is_not_an_ai Jun 21 '22 at 12:20
  • @463035818_is_not_a_number Thanks. That helped me understand why my example wouldn't work. –  Jun 21 '22 at 12:26
  • @SamuelAmmoniusSTUDENT but you also need to understand that your example won't work even if the compiler will inline calls to the function – 463035818_is_not_an_ai Jun 21 '22 at 12:29
  • Just to add another unknown : The address of num (argument of the function) might even depend on the calling convention and/or architecture of your CPU (what is the address of a register?). So all in all enough reason for this not to work. On a side note, getting the address of something has a standard library implementation [std::addressoff](https://stackoverflow.com/questions/16195032/implementation-of-addressof) – Pepijn Kramer Jun 21 '22 at 12:30
  • *is the answer "11" or "segmentation fault"?* Neither, either, or possibly both. That is the supreme joy and mystery of **undefined behavior**. It could appear to work. It could crash. It could do something else. It could email your browser history to your grandmother. – Eljay Jun 21 '22 at 13:46

2 Answers2

3

inline does not mean that a function will be inlined. This was always only a hint to the compiler.

Since C++17 this has been codified in the standard and inline now basically just means "multiple definitions are permitted".

Read the details on https://en.cppreference.com/w/cpp/language/inline

And even if the function is inlined it still has to behave as-if it wasn't inlined. Returning the address of an argument is UB, inlined or not.

Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
  • I don't think there was any change in meaning of inline functions on C++17. As far as I know it's been the same since first standard version. What codification are you referring to? – eerorika Jun 21 '22 at 13:24
  • @eerorika Specifically *Because the meaning of the keyword inline for functions came to mean "multiple definitions are permitted" rather than "inlining is preferred", that meaning was extended to variables.* – Goswin von Brederlow Jun 21 '22 at 13:47
2

Whether a function is inline has no effect on whether it's ok to return an address of any value.

It can be sometimes ok to return address of an rvalue if it is an xvalue that refers to an object outside the function.

num is not an rvalue expression. Returning an address of a function parameter of object type is never useful. The example function returns an invalid pointer and the behaviour of the program is undefined.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
eerorika
  • 232,697
  • 12
  • 197
  • 326