Hi All

Was just working on detecting memory leaks .......

Where can i get the actual code implemention of new and delete functions in C++ ,

Best Regards

Varun

Recommended Answers

All 5 Replies

Member Avatar for iamthwee

Hi All

Was just working on detecting memory leaks .......

Where can i get the actual code implemention of new and delete functions in C++ ,

Best Regards

Varun

I think you can get it from google.

>Was just working on detecting memory leaks
Programmatically or are you just working on setting up guidelines?

>Where can i get the actual code implemention of new and delete functions in C++
Wait, what? This varies with every implementation, and the internal mechanism should be largely irrelevant even for a memory leak detection program. How about you tell us what you're trying to accomplish rather than ask for something that probably won't do you any good.

HI

Actually , i was trying to oveload the new and delete operators ...with my own functions , so that every time a call to new is made , i can record the memory chunk allocated , and then finally delete the same , when delete is called . Now what i was doin' in my new 'new' function - was mere call to malloc ...Is that sufficient ???

Best Regards

Varun

I would suggest that you call the super implementation of new and delete , if that's possible. You must know that these operators are not mere malloc 's and free 's. Apart from Googling the source for these operators, you can run the debugger in your IDE and step into the code where you call new or delete to see the source (I know Microsoft Visual Studio provides the source).

Hi

I just started off with a small prog to see if i could implement the same ..

*************************************************************************************
#include<iostream.h>
#include<stdlib.h>

void * operator new(size_t size)
{

    void * ptr;
    cout<<"Entering New()"<<endl;
    ptr=malloc(size);
    cout<<"Allocated Location:"<<ptr<<endl;

    cout<<"Exiting New()"<<endl;
    return (ptr);


}

void operator delete(void *p)
{

    cout<<"Entering Delete()"<<endl;
    free(p);
    cout<<"Address freed"<<p<<endl;

    cout<<"Exiting Delete()"<<endl;
}

void main()
{

    cout<<"Entering Main()"<<endl;
    int *ptr1;
    //ptr=new int;
    ptr1=new int;
    *ptr1=2;
    cout<<*ptr1<<endl;

    delete ptr1;
    cout<<*ptr1<<endl;


    cout<<"Exiting Main()"<<endl;


}

********************************************************************

OUTPUT

Entering Main()
Entering New()
Allocated Location:0x1400047a0
Exiting New()
2
Entering Delete()
Address freed0x1400047a0
Exiting Delete()
2
Exiting Main()
Entering Delete()
Address freed0x140004500
Exiting Delete()

*************************************************************8

Could anyone explain me , why the delete is being called twice ????

Also , Can i extend this basic module to record all the details of allocation and deallocation ??

Best Regards

Varun

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.