Check for memory leaks

Source: Internet
Author: User

1. Allocating space

2. Log memory block information

3. Calling constructors (type extraction)

#include <iostream> #include <string> #include <list> #include <assert.h>using namespace  std;struct BlockInfo{void* _ptr;string _file;int _line; Blockinfo (Void *ptr, const char*file, int line): _ptr (PTR), _file (file), _line (line) {}}; List<blockinfo> blocklist;void* alloc (Size_t size, const char*file, int  line) {void* ptr = malloc (size);if  (PTR) {blockinfo  b (ptr, file,  line); Blocklist.push_back (b);} Return ptr;} Void  dalloc (void *ptr) {free (PTR);//[) list<blockinfo>::iterator it =  Blocklist.begin ();while  (It != blocklist.end ()) {if  (it->_ptr == ptr) { Blocklist.erase (it); return;} ++it;} ASSERT (FALSE);} Void print () {cout <<  "Memory leak memory block"  << endl;list<blockinfo>::iterator  it = blocklist.begin ();while  (it != blocKlist.end ()) {printf ("ptr:%p file:%s line:%d\n",  it->_ptr, it->_file.c_str (),  It->_line); ++it;}} Template<class t>void *__new (size_t size, const char* file, int  Line) {Void *ptr=alloc (size, file, line);if  (Typetraits <t>::__ispodtype (). Get ()) return ptr;elsereturn new (PTR)  t;} Template<class t>void __delete (T&NBSP;*PTR) {if  (! Typetraits <t>::__ispodtype (). Get ()) ptr->~t ();D alloc (PTR);} Template<class t>void *__new_array (size_t size, int num,const char*  File, int line) {void *ptr = alloc (size, file, line); * ((int*) PTR)  =  num;ptr =  (void*) ((int) ptr + 4); t*cur =  (t*) ptr;if  (! Typetraits <t>::__ispodtype (). Get ()) {for  (int i = 0; i < num; i++) {new (cur) t;++cur;}} Return&nbsP;ptr;} Template<class t>void __delete_array (void* ptr) {int num = * ((int*) PTR &NBSP;-&NBSP;1); t* cur =  (t*) ptr;if  (! Typetraits <t>::__ispodtype (). Get ()) {for  (int i = 0; i < num; i++) {cur->~t (); ++cur;}} Dalloc ((void*) ((int) ptr-4)); Struct __truetype{bool get () {return true;}}; Struct __falsetype{bool get () {return false;}}; template <class _tp>struct typetraits{typedef __falsetype   __ Ispodtype;}; template <>struct typetraits< bool>{typedef __truetype      __IsPODType;}; template <>struct typetraits< char>{typedef __truetype      __IsPODType;}; template <>struct typetraits< int>{typedef __truetype      __IsPODType;}; #define &NBSP;NEW (Type) &NBSP;__NEW&LT;type> (sizeof (type), __file__,__line__) #define &NBSP;DELETE (type,ptr)  __DELETE<type> (PTR) #define  new_array (type,num)   __NEW_ARRAY<type> (sizeof (type) *num+4,num,__file__,__line__) #define   delete_array (type,ptr)   __DELETE_ARRAY<type> (PTR)

#include <iostream>

using namespace Std;

#include "memory.hpp"

Basic type

void Test1 ()

//{

int *p1 = (int*) NEW (sizeof (int));

int *p2 = (int*) NEW (sizeof (int));

int *p3 = (int*) NEW (sizeof (int));

DELETE (p1);

DELETE (p2);

DELETE (p3);

//}

Custom Types

void Test2 ()

//{

String *s1 = (string*) NEW (string);

*s1 = "ABCD";

cout << s1->c_str () << Endl;

DELETE (STRING,S1);

//}

Primitive Type Array

void Test3 ()

//{

int *p1 = (int*) new_array (int, 10);

*p1 = 2;

p1[1] = 3;

int *p2 = (int*) new_array (int, 10);

delete_array (int, p1);

delete_array (int, p2);

//}

Custom Type Array

void Test4 ()

//{

string* p1 = (string*) New_array (string, 5);

p1[0] = "ABC";

P1[1] = "Def";

p1[2] = "xxx";

p1[3] = "LLL";

p1[4] = "www";

Delete_array (String, p1);

//}

void Test5 ()

//{

//

int *p1 = (int*) new_array (int, 10);

int *p2= (int*) new_array (int, 10);

int *p3 = (int*) new_array (int, 10);

int *p4 = (int*) new_array (int, 10);

p1[0] = 1;

delete_array (int, p1);

delete_array (int, p2);

delete_array (int, p3);

delete_array (int, p4);

//}

int main ()

//{

Test4 ();

//test5 ();

//Register a function to execute after the completion of the main function, function limit void Func (void)

atexit (Print);

System ("pause");

return 0;

//}




This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1728574

Check for memory leaks

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.