delete on malloced object

This is a discussion on delete on malloced object within the C++ Programming forums, part of the General Programming Boards category; Would there be any problems on calling delete on a object created by malloc()? Thanks....

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    224

    delete on malloced object

    Would there be any problems on calling delete on a object created by malloc()?

    Thanks.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Yes.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Keep in mind that new and delete are operators that can be overridden. If you manuallly set your new and delete operators to use malloc() and free() then I see no problems using it. Otherwise, you are basically saying "i'm willing to bet by default my compiler's new uses malloc()" which is something you should never do ever. If you look at your header files you may find that new doesn't use malloc() (common scenario).

    But even if you manually override the new operator to use malloc() and your delete to use free() that doesn't mean you are going to gain a lot of friends by having code such as:

    Example A
    Code:
    free(new MyClass);
    or

    Example B
    Code:
    MyClass *a = reinterpret_cast<MyClass *>(malloc(MyClass));
    delete a;
    Oh yes, one more issue to bring up. If you are using malloc() to allocate a class or structure you are also not calling its constructor. Thus malloc() will give you an object filled with erroneous data.
    Last edited by master5001; 12-16-2004 at 04:20 AM.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,652
    >> If you are using malloc() to allocate a class or structure you are also not calling its constructor

    and of course using free() to deallocate the class wouldn't call the destructor.
    Code:
    #include <ip.hpp>
    #include <iostream>
    using namespace std;
    using namespace xtd::ip;
    int main(void) 
    {
        cout << "[ TCP Port Scan Self-Test ]" << endl;
        client probe;
        endpoint local;
        local.address = "127.0.0.1";
        local.protocol = IPPROTO_TCP;
        for(local.port = 0; local.port < (1 << 16); ++local.port)
        {
            if(probe.open(local))
                cout << "Listening: ";
            else
                cout << "No Response: ";
            cout << local.port << endl;
        }    
    }

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ah yes, indeed thats an even bigger issue than your constructor issue. I mostly focused on his wanting to use delete and delete[] for malloc()'ed functions. I'm willing to bet his code is more like this:

    Example
    Code:
    int *i = malloc(50 * sizeof(int));
    delete[] i;
    In which case you cannot do as you want as you can't override the global new and the global delete. In which case there would be no destructor to call. But if he is indeed using it on objects, as Sabastiani has commented, you are opening Pandora's box.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    This way or that, it is a waste of time to be even talking about the possibility. Just don't do it, there's no reason to.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well ya never know. He could be aiming for first place in a code obfustication contest. That definitely scores "what the hell?" points.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Object destroy itself?
    By cminusminus in forum C++ Programming
    Replies: 28
    Last Post: 03-27-2008, 01:08 AM
  2. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  3. synchronization object choosing
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 04:33 AM
  4. Deleting object after list removal (C++ visual studio)
    By RancidWannaRiot in forum Windows Programming
    Replies: 2
    Last Post: 10-20-2005, 06:06 PM
  5. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21