Mail Archives: djgpp/1997/05/28/16:33:18
Nathan Gray <gray AT ll DOT mit DOT edu> wrote:
> Since we're on the subject of C and C++, does anyone have a good
> explanation for the differences between malloc() and new? As a native
> C++ programmer, my first instinct is to use new, but I see malloc() so
> often I'm starting to wonder if I'm missing something.
As the Dr. Andras stated new calls to the constructor, you won't get that from malloc.
For something like:
new int[1000]
there is no difference with:
(int *)malloc(sizeof(int)*1000)
because gcc calls to an internal routine for new that calls to malloc.
A similar thing is for delete/free.
BUT!,
1) This can be different in another compiler.
2) The first version is more easy and compact.
3) new and delete can be overloaded and have some special features, in this way
you can change the behavior of the program without important changes (I used
that on Borland to catch delete NULL cases).
4) The built in new and free of gcc have some extra checks (free(NULL) is hence
checked twice ;-).
SET
> --
> | Nathaniel A. Gray | MIT Lincoln Laboratory |
> | gray AT ll DOT mit DOT edu | Group 35 - Signature Studies and Analysis|
> |------------------------------------------------------------------|
> | "But don't panic! Base eight is just like base ten, really... |
> | if you're missing two fingers!" -Tom Lehrer |
>
>
------------------------------------ 0 --------------------------------
Visit my home page: http://www.geocities.com/SiliconValley/Vista/6552/
Salvador Eduardo Tropea (SET). (Electronics Engineer)
Address: Curapaligue 2124, Caseros, 3 de Febrero
Buenos Aires, (1678), ARGENTINA
TE: +(541) 759 0013
- Raw text -