In Teach Yourself C++ in 21 Days there is an
exercise that asks the reader to create a program
that produces a memory leak. In the answers section
of the book, it says that this is the right answer:
Anyway, both functions print the same address.Code://Prototype int *Function(); //Main int main() { int *pMem = Function(); cout<<"The Value of pMem in main is: " <<pMem <<"\n"; return 0; } //Function int *Function() { int *pMem = new int; cout<<"The value of pMem in Function is: "<<pMem <<"\n"; return pMem; }
How come the book says this is a Memory Leak?
Why can't I call delete on main's pMem and free
the memory? They both have the same address...
I can't seem to understand this. I don't suppose
there is any chance that the book is wrong, is
there?
Anyway, it's exercise 6 on Day 9 if you have the
book.