mixing operator new, free()

Discussion in 'C++' started by Matt Garman, Apr 2, 2004.

  1. Matt Garman

    Matt Garman Guest

    If I have a C++ function that returns dynamically allocated memory using
    the new operator, must that memory be freed using the delete operator,
    or is it okay to use free()?

    My function will have a C wrapper interface. Since I can't use delete
    in C, is it okay to just use free()? Or should I go ahead and write
    another C++ function that will free the memory using delete?

    My instincts tell me it's probably bad to mix new and free(), if not for
    technical reasons, then for code readability reasons.

    Thanks!
    Matt

    --
    Matt Garman
    email at: http://raw-sewage.net/index.php?file=email
    Matt Garman, Apr 2, 2004
    #1
    1. Advertising

  2. Matt Garman

    Leor Zolman Guest

    On Fri, 02 Apr 2004 18:54:22 GMT, Matt Garman <> wrote:

    >If I have a C++ function that returns dynamically allocated memory using
    >the new operator, must that memory be freed using the delete operator,
    >or is it okay to use free()?


    It must be freed using delete to be compliant.

    >
    >My function will have a C wrapper interface. Since I can't use delete
    >in C, is it okay to just use free()? Or should I go ahead and write
    >another C++ function that will free the memory using delete?


    If by having a "C wrapper interface" you mean it will be callable from C
    programs, you can do that while the function is still compiled by a C++
    compiler...so it can perform the delete directly. Here's an old clc thread
    that explains the issue:
    http://dbforums.com/arch/88/2002/10/534303

    >
    >My instincts tell me it's probably bad to mix new and free(), if not for
    >technical reasons, then for code readability reasons.


    Your instincts are on the right track. Actually, it is /definitely/ bad...
    -leor

    >
    >Thanks!
    >Matt


    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: Download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
    Leor Zolman, Apr 2, 2004
    #2
    1. Advertising

  3. "Matt Garman" <> wrote in message
    news:-sewage.fake...
    > If I have a C++ function that returns dynamically allocated memory using
    > the new operator, must that memory be freed using the delete operator,
    > or is it okay to use free()?


    You must use delete.

    >
    > My function will have a C wrapper interface. Since I can't use delete
    > in C, is it okay to just use free()?


    No, you must use delete.

    > Or should I go ahead and write
    > another C++ function that will free the memory using delete?


    Sounds reasonable.

    But of couse the other option would be to use malloc from C++, then you can
    use free from C. Since you are obviously writing C++ code to be called from
    C why not do this?

    >
    > My instincts tell me it's probably bad to mix new and free(), if not for
    > technical reasons, then for code readability reasons.
    >
    > Thanks!
    > Matt


    john
    John Harrison, Apr 2, 2004
    #3
  4. Matt Garman

    nobody Guest

    "Matt Garman" <> wrote in message
    news:-sewage.fake...
    > If I have a C++ function that returns dynamically allocated memory using
    > the new operator, must that memory be freed using the delete operator,
    > or is it okay to use free()?
    >
    > My function will have a C wrapper interface. Since I can't use delete
    > in C, is it okay to just use free()? Or should I go ahead and write
    > another C++ function that will free the memory using delete?
    >

    IMHO you definitely should. Follow (both C and C++) semantics. If you offer
    an interface for allocating memory, you should also offer matching one for
    deallocating
    it. Users of your code shouldn't worry whether to use free(), delete, or
    delete [].
    Especially if you don't provide source code. And you can change
    implementation
    later: malloc()/free() vs. new/delete without affecting user's code.

    > My instincts tell me it's probably bad to mix new and free(), if not for
    > technical reasons, then for code readability reasons.
    >
    nobody, Apr 3, 2004
    #4
  5. > My instincts tell me it's probably bad to mix new and free(), if not for
    > technical reasons, then for code readability reasons.
    >
    > Thanks!
    > Matt
    >

    Bjarne Stroustroup tells us in the 2nd edition of his book that
    malloc/new and free/delete cannot be mixed, if my memory serves me right.

    Nevertheless, some compilers generate a wrapped malloc/free call if they
    see new/delete (e.g. WATCOM C/C++ for DOS around 1994).

    The fastest way is to write your own wrapper. Otherwise you have to dig
    into your compiler internals.

    --
    Best regards,
    Alex.

    PS. To email me, remove "loeschedies" from the email address given.
    Alexander Malkis, Apr 4, 2004
    #5
  6. In message <c4pqt0$1en10$-saarland.de>, Alexander Malkis
    <-sb.de> writes
    >> My instincts tell me it's probably bad to mix new and free(), if not for
    >> technical reasons, then for code readability reasons.
    >> Thanks!
    >>

    >Bjarne Stroustroup tells us in the 2nd edition of his book that
    >malloc/new and free/delete cannot be mixed, if my memory serves me
    >right.
    >


    Since malloc knows nothing about constructors, nor free about
    destructors, that would be the logical conclusion.

    >Nevertheless, some compilers generate a wrapped malloc/free call if
    >they see new/delete (e.g. WATCOM C/C++ for DOS around 1994).


    10 years is a long time in C++ terms. How did they deal with calling
    constructors and destructors for the resulting objects?
    >
    >The fastest way is to write your own wrapper.


    First write a contract. Which side of the C/C++ interface owns the
    allocated objects; who is responsible for deleting them; is it
    exception-safe?

    >Otherwise you have to dig into your compiler internals.


    which would pretty much guarantee UB and non-portable code :-(.

    --
    Richard Herring
    Richard Herring, Apr 8, 2004
    #6
  7. > Bjarne Stroustroup tells us in the 2nd edition of his book that
    > malloc/new and free/delete cannot be mixed, if my memory serves me right.
    >

    Which it won't if you use malloc with delete or new with free :)
    Geoffrey Mortimer, Apr 8, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jef Driesen
    Replies:
    1
    Views:
    492
    Gernot Frisch
    Jan 19, 2005
  2. Replies:
    8
    Views:
    322
    Neil Cerutti
    Dec 22, 2005
  3. george
    Replies:
    0
    Views:
    1,087
    george
    Aug 29, 2008
  4. xmllmx
    Replies:
    6
    Views:
    405
    xmllmx
    Feb 3, 2010
  5. mohammed_a_o
    Replies:
    0
    Views:
    260
    mohammed_a_o
    Nov 30, 2010
Loading...

Share This Page