Thanks for your reply. My question really has to do with the
difference between an allocation function like malloc, HeapAlloc, etc.
and the new operator.
Your next question, you mean? That doesn't sound much like the first one...
Both allocate space for the class object off
the free store or heap.
Right, and the default behavior of new may very well be to use malloc
internally, but code would be non-conformant if it actually assumed that
and attempted to do something that relied upon it (such as free() the
memory.)
The new operator additionally calls any
constructor defined for the class (and any in the inheritance chain).
Well, just think of it as calling "the" constructor for the object being
instantiated (or objects in an array). The fact that the constructor may
call others is somewhat tangential.
That's the only difference I can think of until I read this article,
http://www.codeproject.com/tips/newandmalloc.asp?print=true
, which explains that new throws an exception on failure rather than
returning an error code (NULL),
True, but that is an "exceptional" condition; i.e., how out-of-memory is
handled doesn't significantly affect normal performance of the allocation
mechanism, and ought only be an issue with respect to choosing the most
appropriate way in your program for dealing with the possibility of running
out of memory. You can even tell new not to throw an exception (but just
return NULL) using the nothrow syntax, eliminating that as a difference, if
you so desire.
and that it's not possible to realloc
space allocated by new.
Yes, well, realloc is rather quirky in its own right, so no great loss
there.
The new operator has always been a little
mysterious to me. There's nothing else going on right?
I don't know how deep you want to go with that question (not that I know
much more about it than what's already been said), but I'd suggest that
when using C++ in general, you stick with new/delete unless you have some
/very/ good reason to use the *alloc/free instead.
I would have used a struct, but the members of my class are only to be
accessible by two other classes and nothing else.
And you've declared them public?
-leor