| | |
The New And Delete Function
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
>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.
New members chased away this month: 5
•
•
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
Views: 2580 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int java lib library lines linkedlist linker loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






