throws different exceptions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • datastrc
    New Member
    • Feb 2008
    • 2

    throws different exceptions

    Hello

    When compiling the following example

    [CODE=cpp]#include <cstdlib> //declarations of malloc and free
    #include <new>
    #include <iostream>
    using namespace std;

    class C {
    public:
    C();
    void* operator new (size_t size); //implicitly declared as a static member function
    void operator delete (void *p); //implicitly declared as a static member function
    };

    void* C::operator new (size_t size) throw (const char *){
    void * p = malloc(size);
    if (p == 0) throw "allocation failure"; //instead of std::bad_alloc
    return p;
    }

    void C::operator delete (void *p){
    C* pc = static_cast<C*> (p);
    free(p);
    }

    int main() {
    C *p = new C; // calls C::new
    delete p; // calls C::delete
    }[/CODE]

    Code:
     g++ -Wall example.cpp -o example
    example.cpp:14: error: declaration of 'static void* C::operator new(size_t) throw (const char*)' throws different exceptions
    example.cpp:9: error: from previous declaration 'static void* C::operator new(size_t)'
    example.cpp: In static member function 'static void C::operator delete(void*)':
    example.cpp:21: warning: unused variable 'pc'

    It something wrong with new operator overloading ?
    What's this mean
    ... operator new(size_t) throw (const char*)' throws different exceptions
    Last edited by Ganon11; Feb 15 '08, 02:44 PM. Reason: Please use the [CODE] tags provided.
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    #2
    For one thing,
    throw ("allocation failure")
    instead of:
    throw "allocation failure"
    and please enclose your code between [CODE=cpp] ... [/CODE] to make it much easier to read for the rest of us.

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Why, why, why are you using malloc and free in a C++ environment, especially with classes? That's what new and delete are for. Using malloc only gets you a chunk of memory, but new calls the constructor of the object. What possible purpose could you have for using these C functions in C++?

      Also: Please don't double post your question. Having two threads doesn't help you - it just confuses the people trying to help you.

      Comment

      • datastrc
        New Member
        • Feb 2008
        • 2

        #4
        I am interrested to understand new/delete operators overloading and actually this code is taken from a tutorial. I am still learning :) ...

        I have a question:
        new operator can be used to allocate a void * pointer ?
        Something like that
        [code=cpp]
        void *v;
        v=new (1024);//v=malloc(1024) works fine
        [/code]
        won't be compilled.

        Originally posted by arnaudk
        For one thing,
        throw ("allocation failure")
        instead of:
        throw "allocation failure"
        and please enclose your code between [CODE=cpp] ... [/CODE] to make it much easier to read for the rest of us.

        Nothing changed even if instead of
        [CODE=cpp]
        throw "allocation failure"
        [/CODE]
        is used
        [CODE=cpp]
        throw ("allocation failure")
        [/CODE]

        [CODE=cpp]
        #include <cstdlib> //declarations of malloc and free
        #include <new>
        #include <iostream>
        using namespace std;

        class C {
        public:
        C();
        void* operator new (size_t size) throw (const char *); //implicitly declared as a static member function
        void operator delete (void *p); //implicitly declared as a static member function
        };

        void* C::operator new (size_t size) throw (const char *)
        {
        void * p = malloc(size);
        if (p == 0)
        throw ("allocation failure") ; //instead of std::bad_alloc
        return p;
        }

        void C::operator delete (void *p){
        C* pc = static_cast<C*> (p);
        free(p);
        }

        int main() {
        C *p = new C; // calls C::new
        delete p; // calls C::delete
        }
        [/CODE]


        Code:
        $g++ -g3 -Wall example.cpp -o example
        
        example.cpp: In static member function 'static void C::operator delete(void*)':
        example.cpp:22: warning: unused variable 'pc'
        /tmp/cc9LdSLe.o: In function `main':
        example.cpp:27: undefined reference to `C::C()'
        collect2: ld returned 1 exit status
        But, oops ! Constructor body is missing.

        [CODE=cpp]
        #include <cstdlib> //declarations of malloc and free
        #include <new>
        #include <iostream>
        using namespace std;

        class C {
        public:
        C(){};
        void* operator new (size_t size) throw (const char *); //implicitly declared as a static member function
        void operator delete (void *p); //implicitly declared as a static member function
        };

        void* C::operator new (size_t size) throw (const char *)
        {
        void * p = malloc(size);
        if (p == 0)
        throw "allocation failure" ; //instead of std::bad_alloc
        return p;
        }

        void C::operator delete (void *p){
        C* pc = static_cast<C*> (p);
        free(p);
        }

        int main() {
        C *p = new C; // calls C::new
        delete p; // calls C::delete
        }
        [/CODE]

        Now everything is compiled OK, without errors.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          This is a rotten example. You reallty need a better tutorial. One that has no malloc() and no type casts.

          Comment

          Working...