- // Memleak. cpp
- # Include <stdlib. h>
- # Include <crtdbg. h>
- Using namespace STD;
- Int main (void)
- {
- // Reg_debug_new; // +
- _ Crtmemstate S1, S2, S3; // declare the memory state structure variable
- _ Crtmemcheckpoint (& S1); // gets the first memory status Snapshot
- // Allocate memory here
- Int * P = new int [2];
- P [0] = 'a ';
- P [1] = 'B ';
- // Delete [] P;
- _ Crtmemcheckpoint (& S2); // gets the second memory status Snapshot
- // Compare the differences between two memory snapshots
- If (_ CrtMemDifference (& s3, & s1, & s2 ))
- _ CrtMemDumpStatistics (& s3); // dump difference result
- // Output the memory leakage Report, which is displayed on the Debug page of the Output Window
- _ CrtDumpMemoryLeaks ();
- Cout <"-- End --" <endl;
- Return 0;
- /*
- REG_DEBUG_NEW; // +
- Char * p = new char [2];
- Cout <"-- End --" <endl;
- Return 0;
- */
- }
The above code can only see the running result in debug!