Say we have a struct that has a member pointer that conditionally points to either an internal array, or to somewhere on the heap, like this:
struct Ex {
char* p;
char data[14];
bool is_heap;
Ex() : p(&data[0]), data(), is_heap(false) {}
//etc...
};
Now consider this function
Ex f1() {return Ex();}
Due to copy elision, the following code will print out "h":
int main() {
auto ex = f1();
ex.data[0] = 'h';
std::cout << ex.p[0];
}
But, consider the following function
Ex f2() {
auto ret = Ex();
return ret;
}
As far as I am aware, this function may be elided, but if it's not, the following code will be undefined behavior:
int main() {
auto ex = f2();
ex.data[0] = 'h';
std::cout << ex.p[0]; // maybe derefrencing dangling pointer, maybe printing out "h"?
}
My question is, is example 2 always undefined behavior? Is it up to the compiler whether it is undefined behavior (as in if it decided to elide or not)? or is it well defined behavior? (same questions may also apply to the first example)