1

Is this code UB or not?

__attribute__((const)) const char* foo() {
 static const char* str = "yo bro";
 return str;   
}

__attribute__((pure)) const char* bar() {
 static const char* str = "ey mate";
 return str;   
}

I'm in particular interested in how pure/const work together with the str declaration.

10
  • 1
    According to this, at least the ((pure)) snippet seems to be well-defined. Commented Jan 1, 2018 at 13:13
  • 1
    I think both are safe. Commented Jan 1, 2018 at 13:47
  • 1
    But why? Why are you not just doing return "you bro"; Commented Jan 1, 2018 at 15:36
  • Also what do you mean by "UB"? Undefined by what? Commented Jan 1, 2018 at 15:39
  • The only thing the __attribute__((pure|const)) says is that the compiler can remember the return value. Nothing else. You can use __attribute__((const)) on even rand, and then have "interesting" results. But the contents of the function itself do not matter. Commented Jan 1, 2018 at 15:53

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.