Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,

Thanks very much in advance.
I am working on C++ project in DevC++ environment.

I am trying to overload "new" and "delete" operators but I am unable to call those overloaded operators. Can you please help me identifying where am I making mistake??

C#
void* operator new (size_t , int )// working
{
      cout<<"This is overloaded-new operator"<<endl;
}

void operator delete (void* pointer, int iData)// working
{
     cout<<"This is overloaded-delete operator"<<endl;
}

C#
void main()
{
// as per my understanding following should call overloaded operators
// following gives error: invalid conversion from int to void*
//          initializing argument 2 of void* operator new[](size_t, void*)
void* chObjNew = new(10)char[10];
// following error: type int argument given to delete expected pointer
     delete(chObjNew, 10) ;
}
Posted

I think you have to overload the array form of new as you are allocating for array

C++
void* operator new[](size_t t) 
{
     // ...
}


and similarly delete

C++
void operator delete[](void* ptr
{
   // .....
}
 
Share this answer
 
Comments
ggupta2009 9-Feb-12 7:17am    
Thank you very much.
In this way "new" is working but somehow "delete" is still giving problem.

when I tried hit-and-trial.

following way code is running fine. could you please explain the situation briefly??

void* operator new[] (size_t t, int a)// working
{
cout<<"This is overloaded-new operator"<<endl;
cout<<"Parameter passed to oveloaded-new is: "<<a<<endl;
}

void operator delete (void* pointer, int iData, int iData1)// working
{
cout<<"This is overloaded-delete operator"<<endl;
cout<<"Parameter passed to oveloaded-new is: "<<iData<<endl;
cout<<"Parameter passed to oveloaded-new is: "<<iData1<<endl;
}

void main()
{
void* chObjNew = new(10)char[1];
::operator delete (chObjNew, 13, 14);
}

Output:
This is overloaded-new operator
Parameter passed to oveloaded-new is: 10
This is overloaded-delete operator
Parameter passed to oveloaded-new is: 13
Parameter passed to oveloaded-new is: 14
Do find excellent explanation with neat exaples at :

http://bytes.com/topic/c/answers/129162-overloading-new-delete-operators[^]
 
Share this answer
 
Comments
ggupta2009 9-Feb-12 7:19am    
Thanks a lot.
Link is helpful.
If you fully qualify your delete it'll work I think:
C++
::operator delete(chObjNew, 10);


Hope this helps,
Fredrik
 
Share this answer
 
Comments
ggupta2009 9-Feb-12 7:19am    
Thank you very much.
::operator delete(chObjNew, 10); is working

Could you please tell why for "new" we need not to provide global-scope-resolution-operator?
Fredrik Bornander 9-Feb-12 7:41am    
I might be going out a limb here but I think they do two different things.
::operator delete deletes the memory allocated to the pointer passed in.
"normal" delete runs the destructor and then runs the ::operator delete.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900