Thread: Question on overloading the operator delete...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124

    Question on overloading the operator delete...

    I'm curious to know how i would get around the static member error.

    Code:
    #include <stdlib.h>
    
    
    struct image
    {     short count;
          unsigned int * num;
          unsigned int ** nums;
          void Init( short number )
          {
                num  = NULL;
                nums = NULL;
                count = number;
                if( number == 1)
                num = (unsigned int *)malloc( sizeof(unsigned int));
                else if ( number > 1 )
                {
                    nums = (unsigned int **)malloc( sizeof(unsigned int) * number);
                    int i;
                    for( i = 0; i < count; ++i)
                    {
                         nums[count] = NULL;
                    }
                }
          }
          
          void operator delete (void *p)
          {
                image * q = (image * )p;
               if( q->count == 1 )
               { free(q->num); num = NULL ; }
               else if( q->count > 1 )
               {
                    int i = 0;
                    for( i = 0; i < q->count; ++i)
                    {
                         if( q->nums[q->count] != NULL )
                         free( q->nums[q->count] );
                    }
                    free( q->nums );
               }
               free(p);
               p = NULL;
          }
          
    };
          
          int main(void)
          {
              image * something;
              something->Init( 1 );
              delete something;
              
              return 0;
          }
    I was trying to make a image struct to hold either 1 array of unsigned integers or an array of images. I wanted to push each structure into a class hash table I built a while back, but I had an issue using delete in my hash table and with the lost of memory from the array of unsigned int. So i thought about using a overloaded method for delete to avoid the problem, and not to reconstruct my hash table just around this one issue.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It sounds like you want to define the destructor rather than overload operator delete. You should also define (or declare as private) the copy constructor and copy assignment operator, and possibly also the default constructor. You might want to change the Init member function into a constructor that takes a short. This constructor might be declared explicit.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    { free(q->num); q->num = NULL ; }
    operator delete is always static, even if you don't declare it so.

    But besides laserlight's remarks, does one image have to be a special case (perhaps this is a minor optimization, but perhaps it isn't), and why would a struct called image store multiple images in the first place (instead of it storing data of one image, and an array of image objects used for multiple images)?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124
    Thanks for the advice, I think i will go with laserlight's advice first just to try it out, but if not I'll just go with changing the hash table template to handle both array of unsigned int and array of unsigned int arrays ( when I take images from one image source and extract to put into those arrays).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Operator overloading question
    By 39ster in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2007, 12:26 PM
  2. constructor overloading question
    By rozner in forum C++ Programming
    Replies: 6
    Last Post: 10-06-2006, 09:54 PM
  3. overloading question
    By ygfperson in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2003, 09:12 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Operator Overloading newbie question
    By Panopticon in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2003, 07:14 PM