943,746 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3881
  • C++ RSS
Mar 24th, 2006
0

The New And Delete Function

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gpta_varun is offline Offline
24 posts
since May 2005
Mar 24th, 2006
0

Re: The New And Delete Function

Quote originally posted by gpta_varun ...
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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 24th, 2006
0

Re: The New And Delete Function

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 27th, 2006
0

Re: The New And Delete Function

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gpta_varun is offline Offline
24 posts
since May 2005
Mar 27th, 2006
0

Re: The New And Delete Function

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).
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
AstroNox is offline Offline
50 posts
since Mar 2006
Mar 27th, 2006
0

Re: The New And Delete Function

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gpta_varun is offline Offline
24 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Speech recognition
Next Thread in C++ Forum Timeline: reading a hexadecimal number





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC