| | |
The New And Delete Function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
>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.
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.
I'm here to prove you wrong.
•
•
Join Date: May 2005
Posts: 21
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Mar 2006
Posts: 50
Reputation:
Solved Threads: 2
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
AstroNox
•
•
Join Date: May 2005
Posts: 21
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- datagrid delete function not auto update (ASP.NET)
- Seg Fault ~ Linked List Delete (C)
- How can i delete unused and unneeded images from a folder using php ? (PHP)
- Function with pointer? (C++)
- Can't delete a folder (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Speech recognition
- Next Thread: reading a hexadecimal number
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






