The New And Delete Function

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2005
Posts: 21
Reputation: gpta_varun is an unknown quantity at this point 
Solved Threads: 0
gpta_varun gpta_varun is offline Offline
Newbie Poster

The New And Delete Function

 
0
  #1
Mar 24th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: The New And Delete Function

 
0
  #2
Mar 24th, 2006
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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,858
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: The New And Delete Function

 
0
  #3
Mar 24th, 2006
>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.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 21
Reputation: gpta_varun is an unknown quantity at this point 
Solved Threads: 0
gpta_varun gpta_varun is offline Offline
Newbie Poster

Re: The New And Delete Function

 
0
  #4
Mar 27th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 50
Reputation: AstroNox is an unknown quantity at this point 
Solved Threads: 2
AstroNox AstroNox is offline Offline
Junior Poster in Training

Re: The New And Delete Function

 
0
  #5
Mar 27th, 2006
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).
Best Regards, God Bless,
AstroNox
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 21
Reputation: gpta_varun is an unknown quantity at this point 
Solved Threads: 0
gpta_varun gpta_varun is offline Offline
Newbie Poster

Re: The New And Delete Function

 
0
  #6
Mar 27th, 2006
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2580 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC