Memory leakage test code

Source: Internet
Author: User

  1. // Memleak. cpp
  2. # Include <stdlib. h>
  3. # Include <crtdbg. h>
  4. Using namespace STD;
  5. Int main (void)
  6. {
  7. // Reg_debug_new; // +
  8. _ Crtmemstate S1, S2, S3; // declare the memory state structure variable
  9. _ Crtmemcheckpoint (& S1); // gets the first memory status Snapshot
  10. // Allocate memory here
  11. Int * P = new int [2];
  12. P [0] = 'a ';
  13. P [1] = 'B ';
  14. // Delete [] P;
  15. _ Crtmemcheckpoint (& S2); // gets the second memory status Snapshot
  16. // Compare the differences between two memory snapshots
  17. If (_ CrtMemDifference (& s3, & s1, & s2 ))
  18. _ CrtMemDumpStatistics (& s3); // dump difference result
  19. // Output the memory leakage Report, which is displayed on the Debug page of the Output Window
  20. _ CrtDumpMemoryLeaks ();
  21. Cout <"-- End --" <endl;
  22. Return 0;
  23. /*
  24. REG_DEBUG_NEW; // +
  25. Char * p = new char [2];
  26. Cout <"-- End --" <endl;
  27. Return 0;
  28. */
  29. }

 

The above code can only see the running result in debug!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.