malloc & tc.

Steve Summit scs at adam.pika.mit.edu
Sun May 14 05:20:34 AEST 1989


In article <8283 at techunix.BITNET> ssreefc at techunix.BITNET (florin coter) writes:
>        extern char a[1000][50];        /* !!! */
>                if( ( *p=malloc( (unsigned)30000 ) ) == NULL )
>                        puuts( "no space" );

I suspect that the "*p=" and "puuts" are typos; I suspect that
the "extern" on the declaration of a is not.  Being external,
this declaration is what is called a "declaring instance," not a
"defining instance."  It allocates no space.  Since a is not
used, the declaration is essentially discarded, ~50K is not set
aside, and sufficient memory remains to satisfy the malloc(30000)
request.  (If a were used, you would get an "undefined symbol"
error at link time, noting the absence of a defining instance.)

Malloc is an extremely important and oft-used routine.  Although
subtle bugs are occasionally noted in new implementations,
it is quite unlikely that a non-NULL pointer to in-use
memory could ever be returned during an out-of-memory condition
(or under any other circumstances, for that matter).  Malloc has
to return NULL when there's no memory.  That's its job.

                                            Steve Summit
                                            scs at adam.pika.mit.edu

P.S. Please be very careful about typos in source postings,
     incorporating the C source file directly into your text
     editor while composing the message, if possible.  Otherwise
     people may correct your typos and overlook the real problem
     (which I don't claim to have discovered either, because the
     posting is still a bit sketchy).



More information about the Comp.lang.c mailing list