7.30(월) C++ - finding memory leak

from Study/C++ 2007/07/31 14:32 view 869406

// memory.cpp
#include
"memchk.h"

 

// finding memory leak

int main()

{

        int* p1 = new int;

        int* p2 = new int;

        int* p3 = new int;

 

        delete p2;

 

//      cout << __FILE__ << endl;     // 컴파일 하는 파일이름

//      cout << __LINE__ << endl;     // 컴파일 하는 LINE NO

 

        return 0;

}

 

//memchk.h

#include <iostream>

using std::cout;

using std::endl;       // .h 에서는 절대로 namespace를 통째로 열지마라!!(격언중하나)

 

#ifdef _DEBUG

 

struct MemInfo

{

        char name[256];        // 화일이름

        int  line;             // new를호출한line no

        void* addr;            // 할당한메모리주소

};

 

MemInfo mem[10000];  // 최대10000개의 메모리 할당을기록( linked list를 사용해도 됨 )

int count = 0;

 

void* operator new( size_t s, char* file, int line )

{

        void* p = malloc( s );

 

        // 배열에 할당정보를 기록한다.

        mem[count].addr = p;

        mem[count].line = line;

        strcpy( mem[count].name, file );

 

        ++count;

 

        return p;

}

 

void operator delete( void* p )

{

        for( int i = 0; i < count; i++ )

        {

               if( mem[i].addr == p )

               {

                       mem[i] = mem[count-1]; // i번째를 제거

                       --count;

                       break;

               }

        }

        // 배열에 없는 정보라면 에러처리를 하는 것도 좋은방법.

        free(p);

}

 

int MAIN();

 

void main()

{

        MAIN();

 

        if ( count == 0 )

        {

               cout << "NO Memory Leak" << endl;

               return;

        }

 

        cout << "Found " << count << " Memory Leak" << endl;

 

        for( int i = 0; i < count; ++i )

        {

               cout << "FILE : " << mem[i].name << endl;

               cout << "FILE : " << mem[i].addr << endl;

               cout << "FILE : " << mem[i].line << endl;

               cout << endl;

        }

}

 

 

// 꼭 외워 두세요. new에 인자를 더 보내주기 위한 좋은 방법

#define new new(__FILE__, __LINE__)

#define main MAIN

 

// C에 적용해보고 싶을때!!

#define malloc(x)      MyMAlloc( x, __FILE__, __LINE__ )

#define free(x)               MyFree(x)

 

#endif // _DEBUG

Tag | ,

Trackback Address :: 이 글에는 트랙백을 보낼 수 없습니다

댓글을 달아 주세요

  1. kevin 2011/12/03 22:05  address  modify / delete  reply

    좋은 소스와 설명 잘 보았습니다.
    감사합니다. ^^

  2. Item_1 2017/08/13 13:33  address  modify / delete  reply

    Thanks for finally writing about >지나간 삶 :
    : 7.30(월) C++ - finding memory leak <Liked it!

  3. Svuotare Casa Molise 2017/08/14 21:25  address  modify / delete  reply

    I comment when I appreciate a post on a blog or if I have something to
    valuable to contribute to the conversation. It is a result of the passion displayed in the article I browsed.
    And after this post 지나간 삶 :: 7.30(월) C++ - finding memory leak.
    I was moved enough to post a thought :-) I actually do have a few questions
    for you if it's allright. Is it simply me or do some of the
    comments appear as if they are left by brain dead people?
    :-P And, if you are posting at other social sites, I would like to
    keep up with everything new you have to post. Could you make a list all
    of your social sites like your Facebook page, twitter feed,
    or linkedin profile?

  4. cheap jerseys China 2017/08/16 14:38  address  modify / delete  reply

    Thanks for finally writing about >지나간 삶 :: 7.30(월) C++ - finding memory leak <Liked it!cheap jerseys China

  5. Misspelled signs Billboards 2017/08/22 15:31  address  modify / delete  reply

    I think everything published made a bunch
    of sense. But, consider this, what if you wrote a catchier title?
    I am not suggesting your content is not good.,
    however suppose you added a post title that makes people want
    more? I mean 지나간 삶 :: 7.30(월) C++ - finding memory leak is kinda vanilla.

    You could peek at Yahoo's home page and see how they create article titles to get people to open the links.

    You might add a related video or a related picture or
    two to grab people interested about what you've got to say.

    In my opinion, it could bring your posts a little livelier.

  6. cavities 2017/08/22 21:03  address  modify / delete  reply

    Thanks for finally talking about >지나간 삶 :
    : 7.30(월) C++ - finding memory leak <Loved it!

  7. liz claiborne 2017/08/24 01:12  address  modify / delete  reply

    I tenbd nnot to leaᴠe a response, however
    after looking at a binch of responses on 지나간 삶 :
    : 7.30(월) C++ - finding memory ⅼeak. I do have a few questions for you if you do not mіnd.

    Is it simply me or does it look aas if like a few off thhe comments
    appeaar as iif they arre written by brain dead people? :-P And, if you are posting on additi᧐nal social sites, I'ⅾ like to
    keep uρ with everythіng fresh уоu hafe to post.

    Would you post a list of every one of your puglic sites like
    yoᥙr twitter feed, Facebook page or linkedin profile?

  8. details 2017/08/25 13:34  address  modify / delete  reply

    I believe everything said was very logical. But, what about this?

    suppose you wrote a catchier post title? I mean, I don't want to
    tell you how to run your website, however what if you added
    a post title that makes people want more? I mean 지나간 삶 :
    : 7.30(월) C++ - finding memory leak is a little plain. You might glance at Yahoo's front page and see how they create news
    headlines to grab people to click. You might add a video
    or a picture or two to grab people interested about everything've written. In my opinion, it might make your blog a little bit more interesting.

  9. cheap basketball jerseys from China 2017/08/28 05:48  address  modify / delete  reply

    Thanks for finally talking about >지나간 삶
    :: 7.30(월) C++ - finding memory leak <Loved it!cheap basketball jerseys from China

  10. ขายอุปกรณ์จักรยาน 2017/08/28 16:26  address  modify / delete  reply

    I think that what you wrote was very reasonable.
    But, think about this, suppose you added a little information? I
    ain't saying your information is not good, however
    suppose you added a title that grabbed folk's attention? I mean 지나간 삶 ::
    7.30(월) C++ - finding memory leak is kinda vanilla.
    You could glance at Yahoo's front page and watch how
    they create article headlines to grab people to click.
    You might try adding a video or a picture or two to grab
    people excited about everything've written.
    Just my opinion, it would bring your posts a little
    bit more interesting.

  11. La-z Boy – Air Bonded Leather Executive Chair – Coffee Brown 2017/08/31 12:11  address  modify / delete  reply

    I don't create a great deal of comments, but I looked at
    a few of the responses on this page 지나간 삶 :: 7.30(월) C -
    finding meory leak. I do havve a couple of questions for
    you if you don't mind. Is it just me or does it look like
    like a few of the responses appear like they are left
    by brsin dead people? :-P And, if you are writing at other social sites,
    I'd like to keep up with you. Would you pokst
    a list of all of alll your soocial community sites like your
    Facebook page, twitter feed, or linkediin profile?

  12. Sequin Bow Pony Tail White 2017/09/02 09:59  address  modify / delete  reply

    I almost never leave a response, however I looked at throujgh a few remarks here 지나간 삶 :
    : 7.30(월) C - finding memory leak. I do have
    a few questions for you if it's okay. Could it
    be onoy me or does itt look as if like a few of these comments come across as if they are coming from brrain dead individuals?

    :-P And, if you are writing at additionql online sites, I'd like to follow you.
    Could you post a list oof all of alll your social
    networking pages like your linkedin profile,
    Facebook page or twitter feed?

  13. Classic 2017/09/03 22:02  address  modify / delete  reply

    Thahks for finally talking abou >지나간 삶:: 7.30(월) C - finding memory leak
    <Liked it!

  14. Kristofer 2017/09/05 10:47  address  modify / delete  reply

    I think everything typed made a lot of sense.
    But, think about this, suppose you added a little content?
    I mean, I don't wish to tell you how to rrun your website, hkwever what if you added a post
    title to possibly grab a person's attention? I mean 지나간 삶
    :: 7.30(월) C - finding memory leak is a little boring.
    Yoou could peek at Yahoo's home page and note hhow they creeate post titles to get peoole
    interested. You might add a video or a picturee or two to
    get people interested about everything've got to say.
    Just my opinion, it could make your posts a little livelier.

  15. wholesale football jerseys free shipping from China 2017/09/06 19:45  address  modify / delete  reply

    Thanks for finally talking about >지나간
    삶 :: 7.30(월) C++ - finding memory leak <Liked it!wholesale football jerseys free shipping from China

[로그인][오픈아이디란?]