1

I have this code:

#include <iostream>

const char* out;

void f() {
    const char* in = "foo";
    out = in;
}

int main(){
    f();
    std::cout << out;
}

I'd like to know whether using out in std::cout << out; is undefined behavior.

In const char* in = "foo";, the literal "foo" is a object? I mean, when the expression is evaluated, an object is created to represent "foo"? If not, the in pointer points to the program's image? How lifetime works here for "foo" and in?

João Paulo
  • 6,300
  • 4
  • 51
  • 80
  • 1
    FWIW: If this _was_ UB, returning any pointer of/to a string literal "local to" the function would be UB for the same reason. – user2864740 May 20 '20 at 18:24

0 Answers0