use DELETE to save memory, can someone proof
I am learning C++ and for the followin开发者_如何学Gog code I don't know how much memory would be waisted if I don't delete *p.
#include <iostream>
using namespace std;
int *getPtrToFive()
{
int *x = new int;
*x = 5;
return x;
}
int main()
{
int *p = getPtrToFive();
cout << *p << endl;
delete p; // ?????????????
}
How can I verify or demonstrate it. I'm using Visual Studio 2008 Express. Hoping my IDE can show the result immediatly.
Cheers,
Considering the below example built on Visual C++ 2010 in debug mode, it is going to be 4 bytes leak. CRT is capable of dumping the leaks if found any. ( i.e., not deallocating the acquired resources from free store )
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
int main()
{
int *rawPtr = new int(100) ;
// Purposefully not deallocating the rawPtr owned resources from free store
_CrtDumpMemoryLeaks() ;
return 0;
}
Output :
...
Detected memory leaks!
Dumping objects ->
{56} normal block at 0x006C3250, 4 bytes long.
Data: <2 > 32 00 00 00
Object dump complete.
The program '[6160] memoryLeaks.exe: Native' has exited with code 0 (0x0).
Use std::auto_ptr
to handle raw pointers. They are capable of efficiently deallocate owned resources, if they go out of scope.
Ex:
void foo()
{
int *rawPtr = new int(100) ;
std::auto_ptr<int> safePtr(rawPtr) ;
// Now no need to use delete statement
} // safePtr out of scope => deallocates the resource it owned safely
// No memory leak
This program won't be as demonstrative as you hope because of the following reason. When you program terminates (that is, when the "main" function returns) all of the memory used by the program, whether it was allocated on the heap or the stack, will be returned to the OS.
So while it is technically true that your program leaks memory by not performing a "delete" after a corresponding "new", that memory is only leaked for the life of the program, which isn't very long in this example.
This is kind of an odd question.
Well in this case it's an int, so by not cleaning it up you'll presumably waste 4 bytes of memory plus whatever overhead of the operating system. If you're running a high performance application or game, you don't want to constantly lose memory every frame, also known as a memory leak.
If it's a simple application where you're not continuously allocating memory, it's not too big of a problem, as when your program is closed your OS should clean up the program memory. Again, just don't leak memory at a constant rate and you should be okay I think.
MSDN has an article on finding memory leaks in non-MFC programs: Finding Memory Leaks Using the CRT Library.
Concretely, your program leaks 4 bytes, as in VC++, sizeof(int) == 4
.
There are two parts to this :
In this application you are wasting no memory, since once your application terminates, after main completes all the memory is cleaned up by the OS - so effectively no real leak.
Assuming that there was more to the application the amount of memory that you are leaking depends on the amount of memory that you allocated with the new, and int this case is is whatever the size of an int is for your platform.
精彩评论