For foo1()
, you return a copy of the local variable, not the local variable itself.
For the other functions, you return a copy of a pointer to a local variable. However, that local variable is deallocated when the function finishes, so you end up with nasty issues if you try to reference it afterwards.