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

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -