Re: operator new (iNumBytes) vs std::malloc(iNumBytes)
On May 10, 11:15 pm, Gerhard Fiedler <geli...@gmail.com> wrote:
James Kanze wrote:
On May 10, 2:12 pm, Andrew Tomazos <and...@tomazos.com> wrote:
Given:
void f(void* p) { ... }
and assuming the operator new has not been overloaded, what
are the concrete differences in behaviour of:
void* p = operator new(iNumBytes);
f(p);
vs
void* p = std:malloc(iNumBytes);
f(p);
That are visible by f?
The main one is that in the first, p must be freed by ::operator
delete, and in the second, by std::free.
But AFAIK that's not visible by f, right? If f were to
de/reallocate the memory, there has to be a convention about
how the memory was allocated, as this is not visible by f.
I guess it depends on what you mean by "visible by f". Given
just a pointer, there's no way f can know if it was allocated by
malloc or by new. Or for that matter, whether it was
dynamically allocated, or points to a local variable. On the
other hand, if p was allocated by operator new, and f attempts
to free it with ::free, there will be undefined behavior. Which
means that the results may be very visible (or invisible---you
just don't know).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34