Gettin line number of the called function

前端 未结 3 1697
长发绾君心
长发绾君心 2020-12-21 11:01

Please let me know if I can do it or not?

I am writing a library that could work to track memory allocation and de-allocation in C++. In short, I am trying to see if

3条回答
  •  余生分开走
    2020-12-21 11:17

    Finally I got an answer by one of similar threads in this forum... The following code worked... Lets say,

    class A
    {
        public:
        char str[1000];
        A()
        {
            strcpy(str,"Default string");
        }
        A(const char* s)
        {
            strcpy(str,s);
        }
    };
    
    void * operator new (unsigned size, char const * file, int line)
    {
        cout << "File = " << file << endl;
        cout << "Line = " << line << endl;
        return( malloc(size));
    }
    #define new new(__FILE__, __LINE__)
    int main()
    {
        A *a = new A("Leak");
        printf("%s",a->str);
        delete a;
        return 0;
    }
    

    Related post where I found the answer... overloading new and delete in c++

提交回复
热议问题