Programming, website development forum Get latest updates by RSS Follow TechnicalTalk on Twitter Follow TechnicalTalk on Facebook 
HomeSearchRecent PostsLoginRegisterContact Us

Username  
Password    
  Forgot your password?  

Pages: [1]   Go Down
 
  Email this topic  |  Print
0 Members and 1 Guest are viewing this topic.

Overloading New and Delete operators in C++

 
webmaster forum
nikon  Offline
Activity
0%
 
New Coder
Posts: 20
Topics: 13
September 10, 2008, 09:47:47 PM

Hello All

I want to overload new and delete operator using c++, I have tried it lot but unable to get any success. have you ever done it ? If yes please give me some hint about it.
« Last Edit: July 12, 2011, 10:50:01 PM by Admin »
 
webmaster forum
jaincool  Offline
Activity
0%
 
Regular Coder
Posts: 50
Topics: 15
September 11, 2008, 01:22:37 AM

There is nothing much difficult in Overloading new and delete operator................

Here is a short tutorial on it.......

Code:

#include <iostream.h>
#include <cstdlib.h>
#include <new>
Class loc {
                int longitude,latitude;
    public:
              loc{}
              loc(int lg,int lt)      {
                       longitude=lg;    
                       latitude=lt;     }
             void show()   {
                     cout<<longitude<<“ “;
                    cout<< latitude<<“\n”;
                                  }
             void *operator new(size_t size);
             void operator delete(void *p);
               };
         Void *loc::operator new(size_t size)
{
  void *p;
   cout << “ in overloaded new\n.;
   p=malloc(size);
   if(!p) {
              bad_alloc ba;
      throw ba;
            }
  return p;
      }


Void loc:;operator delete(void *p)
{
   cout << “in overloaded delete “;
   free(p);
}


Int main()
   {
       loc *p1,*p2;
  try {      p1=new loc(10,20);  }
  catch(bad_alloc xa) {
         cout<<“Allocation error for p1\n”;
          return 1;             }

 try {  p2=new loc(-10,-20);  }
  catch (bad_alloc xa) {
cout<<“Allocation error for p1\n”;
         return 1;              }

p1->show();
p2->show();
delete p1;
delete p2;

return 0;
}


« Last Edit: July 12, 2011, 10:49:31 PM by Admin »
 
webmaster forum
msdnguide  Offline
Activity
0%
 
New Coder
Posts: 19
Topics: 1
WWW
May 27, 2011, 04:48:32 AM

jaincool has given perfect example.  It works this way

Visual Studio
 
webmaster forum
john14317  Offline
Activity
0%
 
Skilled Coder
Posts: 149
Topics: 0
May 30, 2011, 03:04:37 AM

check the simple example .......

Code:

class Myclass
{
public:
        void* operator new(size_t);
        void operator delete(void*);
};

void* Myclass::operator new(size_t size)
{
    void *storage = malloc(size);
    if(NULL == storage) {
            throw "allocation fail : no free memory";
    }
}

« Last Edit: July 12, 2011, 10:50:25 PM by Admin »

payday loans
instant online cash advance
 
webmaster forum
kellylsn  Offline
Activity
0%
 
Professional Coder
Posts: 229
Topics: 7
August 04, 2011, 12:42:46 AM

We can't overload new and delete operators.

Payday Loan
Online Easy Loans
 
webmaster forum
dianna  Offline
Activity
0%
 
Regular Coder
Posts: 88
Topics: 10
WWW
August 05, 2011, 07:07:32 PM

Operators
The new operator throws std::bad_alloc, when the allocation fails.
Code:
namespace std
{
    class bad_alloc : public exception { /* ... */ };
}
// normal new and delete throws bad_alloc
void* operator new(size_t)    throw(bad_alloc);
void  operator delete(void *) throw();

Pakistan's Local Search engine
 
  Email this topic  |  Print
Pages: [1]   Go Up
 
Jump to:  



Powered by SMF 1.1.15 | SMF © 2011, Simple Machines


Google visited last this page February 02, 2012, 03:21:11 PM