[comp.sys.amiga.tech] Help. malloc

ragg0270@uxa.cso.uiuc.edu (Richard Alan Gerber) (11/16/90)

Just when I think I'm beginning to get a glimmer of understanding concerning the
C language, I realize I know nothing at all:

I'm trying to port some NCSA HDF software to the Amiga. One header file
defines:
char *malloc();

When I try to use that header file as well as stdlib.h from SAS/C 5.10,
I get an external item mismatch error.

So I look in stdlib.h. There I see:
extern void *malloc __ARGS((unsigned));

What's going on? What is this trying to tell me that malloc returns? It has to
return a pointer to a memory location, right? I must be missing something.
Perhaps this declaration aligns the memory so you can cast the pointer to any
pointer type? Still, the manual says it returns a char *.

SAS tech support says
that whatever's in the include file is correct and the manual is most likely
wrong.

Totally lost,
Richard Gerber
gerber@rigel.astro.uiuc.edu

dillon@overload.Berkeley.CA.US (Matthew Dillon) (11/18/90)

In article <1990Nov15.164228.20119@ux1.cso.uiuc.edu> ragg0270@uxa.cso.uiuc.edu (Richard Alan Gerber) writes:
>Just when I think I'm beginning to get a glimmer of understanding concerning the
>C language, I realize I know nothing at all:
>
>I'm trying to port some NCSA HDF software to the Amiga. One header file
>defines:
>char *malloc();
>
>When I try to use that header file as well as stdlib.h from SAS/C 5.10,
>I get an external item mismatch error.
>
>So I look in stdlib.h. There I see:
>extern void *malloc __ARGS((unsigned));
>
>What's going on? What is this trying to tell me that malloc returns? It has to
>return a pointer to a memory location, right? I must be missing something.
>Perhaps this declaration aligns the memory so you can cast the pointer to any
>pointer type? Still, the manual says it returns a char *.

    malloc returns a 'void *' ... note that this is NOT a 'void', but a
    pointer to a 'void'.  Under ANSI, 'void *' is DEFINED to mean a
    'pointer to anything' and malloc is defined to return a pointer to
    any type of structure.  Thus, under ANSI, you can say:

    struct foo *foop = malloc(sizeof(struct foo));

    And the compiler will not give you a warning.  If malloc were declared
    as returning a 'char *', the compiler would give you warning for the
    above declaration.

						-Matt

>Totally lost,
>Richard Gerber
>gerber@rigel.astro.uiuc.edu

--


    Matthew Dillon	    dillon@Overload.Berkeley.CA.US
    891 Regal Rd.	    uunet.uu.net!overload!dillon
    Berkeley, Ca. 94708
    USA