On Thu, Apr 01, 1999 at 12:13:28PM -0800, Adam Wiggins wrote:
> > Let's say I want to dynamically allocate some memory to an object that
> > I am creating using malloc(). Traditionally I do the following:
> >
> > ObjectType *objectpointer; /* Decl. of pointer to object */
> >
> > objectpointer = (ObjectType *)malloc(sizeof(ObjectType));
> >
> > My question. Do I really need to add the type cast (ObjectType *)
> > before the call to malloc() ?
> >
> > Traditionally I have always been taught to include the cast. However,
> > some references say that a cast was ONLY required in earlier versions
> > of C prior to standard ANSI C. Nonetheless I always type cast as that
> > is how I have done it for years and years. Do I really need the cast?
> > More precisely, do I really need the cast using gcc?
>
> You don't *need* the cast, but it will produce a warning, since malloc
> returns a void pointer. I would get in the habbit of using the cast.
Only if you don't include <stdlib.h>. The cast hides the fact that you
haven't included <stdlib.h>.
Kurt