C ++ code detection

Source: Internet
Author: User

Code_test.h

/* * code_test.h * *  Created on: 2013-3-28 *      Author: hp */#ifndef CODE_TEST_H_#define CODE_TEST_H_#include <map>#include "windef.h"using namespace std;struct ALLOC_INFO{DWORD address;DWORD size;char file[64];DWORD line;};class MyTestMalloc{public:static void CoutResult();static void AddTrack(DWORD addr, DWORD asize, const char *fname,DWORD lnum);static void RemoveTrack(DWORD addr, const char *fname, DWORD lnum);static int malloc_free_sub;static map<DWORD, ALLOC_INFO>* alloc_map;};//Redefined functionsvoid* mallocb(size_t size, const char* pszFile, int nLine);void* reallocb(void* memblock, size_t size, const char* pszFile, int nLine);void* callocb(size_t num, size_t size, const char* pszFile, int nLine);char* strdupb(const char* chs, const char* pszFile, int nLine);void  freeb(void* memblock, const char* pszFile, int nLine);//For C++void* operator new(size_t size, const char* pszFile, int nLine);void* operator new[](size_t size, const char* pszFile, int nLine);void operator delete(void* memblock) throw();void operator delete[](void* memblock) throw();#endif /* CODE_TEST_H_ */

Detect. h

#ifndef DETECT_H#define DETECT_H#ifdef __QX_DEBUG_CODE_TEST__#include "code_test.h"//Redefine new and delete#define new new(__FILE__, __LINE__)//Redefines#define malloc(size) mallocb(size, __FILE__, __LINE__)#define realloc(memblock, size) reallocb(memblock, size, __FILE__, __LINE__)#define calloc(num, size) callocb(num, size, __FILE__, __LINE__)#define strdup(ch) strdupb(chs, __FILE__, __LINE__)#define free(memblock) freeb(memblock, __FILE__, __LINE__)#endif#endif

Code_test.cpp

/* * code_test.cpp * *  Created on: 2013-3-28 *      Author: hp */#include "code_test/code_test.h"#include <stdio.h>#include <stdlib.h>#include <malloc.h>int MyTestMalloc::malloc_free_sub = 0;map<DWORD, ALLOC_INFO>* MyTestMalloc::alloc_map = 0;void MyTestMalloc::AddTrack(DWORD addr, DWORD asize, const char *fname, DWORD lnum){MyTestMalloc::malloc_free_sub++;if (!MyTestMalloc::alloc_map){MyTestMalloc::alloc_map = new map<DWORD, ALLOC_INFO>;}ALLOC_INFO info;memset(&info, 0, sizeof(info));info.address = addr;strncpy(info.file, fname, 63);info.line = lnum;info.size = asize;MyTestMalloc::alloc_map->insert(make_pair(addr, info));}void MyTestMalloc::RemoveTrack(DWORD addr, const char *fname, DWORD lnum){//printf("size:%d\n", MyTestMalloc::alloc_map->size());if (MyTestMalloc::alloc_map->size() == 0){return;}size_t t = MyTestMalloc::alloc_map->erase(addr);if(t == 1){MyTestMalloc::malloc_free_sub--;}}void MyTestMalloc::CoutResult(){printf("\n---------check memory-------------------------------------- \n");printf("malloc_free_sub:%d\n", malloc_free_sub);DWORD totalSize = 0;char buf[128];map<DWORD, ALLOC_INFO>::iterator iter = MyTestMalloc::alloc_map->begin();for (; iter != MyTestMalloc::alloc_map->end(); iter++){memset(buf, 0, 128);sprintf(buf, "%s\t line:%ld\t address:%ld\t unfreed:%ld ", iter->second.file,iter->second.line, iter->second.address, iter->second.size);printf(buf);totalSize += iter->second.size;}memset(buf, 0, 128);sprintf(buf, "\n\nTotal Unfreed: %ld bytes ", totalSize);printf(buf);printf("\n-----------------------------------------------------------\n");if(MyTestMalloc::alloc_map){delete MyTestMalloc::alloc_map;}}void* mallocb(size_t size, const char *pszFile, int nLine){void* ptr = malloc(size);MyTestMalloc::AddTrack((DWORD)ptr, size, pszFile, nLine);return ptr;}void* reallocb(void *memblock, size_t size, const char *pszFile, int nLine){    void * ptr;    ptr = realloc(memblock, size);    MyTestMalloc::RemoveTrack((DWORD)memblock, pszFile, nLine);    MyTestMalloc::AddTrack((DWORD)ptr, size, pszFile, nLine);    return ptr;}void* callocb(size_t num, size_t size, const char *pszFile, int nLine){void* ptr = calloc(num, size);MyTestMalloc::AddTrack((DWORD)ptr, size, pszFile, nLine);return ptr;}char* strdupb(const char* chs, const char* pszFile, int nLine){char* ptr = strdup(chs);MyTestMalloc::AddTrack((DWORD)ptr, strlen((char*)chs), pszFile, nLine);return ptr;}void freeb(void *memblock, const char *pszFile, int nLine){MyTestMalloc::RemoveTrack((DWORD)memblock, pszFile, nLine);free(memblock);}void pre_delete(const char* pszFile, int nLine){}void* operator new(size_t size, const char* pszFile, int nLine){void* ptr = malloc(size);MyTestMalloc::AddTrack((DWORD)ptr, size, pszFile, nLine);return ptr;}void* operator new[](size_t size, const char* pszFile, int nLine){void * ptr = malloc(size);MyTestMalloc::AddTrack((DWORD)ptr, size, pszFile, nLine);return ptr;}void operator delete(void* memblock) throw(){if (memblock){MyTestMalloc::RemoveTrack((DWORD)memblock, "", -1);}free(memblock);}void operator delete[](void* memblock) throw(){if (memblock){MyTestMalloc::RemoveTrack((DWORD)memblock, "", -1);}free(memblock);}

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.