×

Loading...

2 is wrong, while 1 and 3 are not so right

syntaxly , 2 is wrong, because it try to return memory on the stack, when v() is finished the the memory on stack is released. the returned pointer ref to invalid memory.
1 and 3 are exactly the same except in debug build 1 will alloc one pointer on stack, in optimized mode, these 2 function will all probably return a constant address,semanticly, 1&3 are all wrong. The string "result of function v()" is allocated by compiler in Initialized data segement, but, notice, logically, it should be constant,although u can modify it.
const char^ v()
{
return "result of function v()";
}
will be the best way
Report

Replies, comments and Discussions: