mixing operator new, free()

M

Matt Garman

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
 
L

Leor Zolman

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
 
J

John Harrison

Matt Garman said:
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
 
N

nobody

Matt Garman said:
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.
 
A

Alexander Malkis

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.
 
R

Richard Herring

Alexander Malkis said:
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 :-(.
 
G

Geoffrey Mortimer

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 :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top