[comp.lang.c] malloc & tc.

ssreefc@techunix.BITNET (florin coter) (05/11/89)

        hello world,
        this is a second posting about malloc. thanks to all who answered.
from the answers i got it is clear to me that it was some missunderstanding.
i'll try this time to be clearer.
THE CODE:

        ....

        extern char a[1000][50];        /* !!! */

        main()
        {
                char *malloc(), *p;

                if( ( *p=malloc( (unsigned)30000 ) ) == NULL )
                        puuts( "no space" );

                ...
        }
        ...
i use turboC 1.5.
in TINY model all one can get is 64K (code, data,...). from the above
it is more than obvious that the request of 30K of space cannot be
satisfied. still malloc does not complain and i get data written over
data. the above is still true for SMALL model where you get 64K for all
data which is more than 64K. still no complain from malloc.
i use microsoft C 3.1.
small model is the same as tc and the program behaves the same way.
WHYYYYYYYYYYYYYYYYYYYY ???????????????????????
florin.

--------
Florin Coter          ssreefc@techunix
Home address:         40/37 Leon Blum St., Haifa, Israel.
Home phone:           04-386133.
Office address:       Solid State Institute, Technion, Haifa 3200, Israel.
Office phone:         04-293938/293613/293615.

rds95@leah.Albany.Edu (Robert Seals) (05/12/89)

ssreefc@techunix.BITNET (florin coter) writes:
>                 if( ( *p=malloc( (unsigned)30000 ) ) == NULL )

Oops. I think you want "p=malloc...", not "*p=malloc...". If you
actually tried to compile this with any warnings on, you'd likely
get a message that p is being used uninitialized. You'd also
get an error because "puuts" is not in the library.

Have you ever used C before?

rob

scs@adam.pika.mit.edu (Steve Summit) (05/14/89)

In article <8283@techunix.BITNET> ssreefc@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@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).