what happens when the function returning value is a pointer and the returning type is a reference in c++? -
guys, got bonus question teacher! helping me !!
the sub2 below not result in run-time error, there may other problem. problem?
enter code here
int& sub2 ( int& , int& b ){ int * pc = new int ; *pc = - b ; return (*pc ) ; }
this function might lead memory leaks.
if user of function relies on signature of function, he'll assume function returns reference object else owns. hence object allocated not released.
of course, if user aware of trick, still delete object taking address of reference
if function used in larger expression (which quite tempting, given signature), such
sub(3, sub(2,1))
not opportunity catch temporary reference.
note function might throw bad_alloc
if there's issue memory allocation.
Comments
Post a Comment