Mail Archives: djgpp/1998/06/09/08:46:31
Gerhard Gruber wrote:
> I heard this a number of times but I don't see any advantage or
> disadvantage.
> When I allocate i.e. a string (like char *x = malloc(n)) I don't see
> the
> reason why I should use new instead of malloc(). Is there a drawback
> to
> malloc()?
If you are using classes with constructors, you will want to use
operator new, since it will automatically call the object's constructor.
You won't want to use malloc in that case.
If you are just allocate blocks of data, you can use either, but you
shouldn't mix-and-match the two allocation methods (i.e., never delete a
block allocated with malloc, and never free a block allocated with new).
The malloc routines still have their use, since you can resize
malloc-allocated blocks in one call with realloc, but can't do an
analogous thing with operator new/delete (unless you do the work
yourself).
--
Erik Max Francis, &tSftDotIotE / mailto:max AT alcyone DOT com
Alcyone Systems / http://www.alcyone.com/max/
San Jose, California, United States / icbm:+37.20.07/-121.53.38
\
She glanced at her watch / . . . It was 9:23.
/ James Clavell
- Raw text -