l i n u x - u s e r s - g r o u p - o f - d a v i s
Next Meeting:
July 7: Social gathering
Next Installfest:
Latest News:
Jun. 14: June LUGOD meeting cancelled
Page last updated:
2003 Mar 21 15:08

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] malloc() is ... old school?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] malloc() is ... old school?

Jeff Newmiller wrote:

> On Fri, 21 Mar 2003, Tim Riley wrote:


> > >
> > >   Especially this one by the author of the FreeBSD C Library (last paragraph):
> > >     http://groups.google.com/groups?q=NULL+calloc()+group:comp.lang.c+author:Chris+author:Torek&selm=18764%40dog.ee.lbl.gov
> Tim, I don't get the impression you actually read this last URL.

Very true -- too many words. My simple mind looks for simple explanations
located in simple sentences.

The truth of the matter is that I've never been
burned using the following code:

char *memory = (char *)0;

if ( !memory )
    printf( "memory is not set\n" );

If instead the code were:

char *memory = (char *)0;

if ( memory != NULL )
    printf( "memory is not set\n" );
    printf( "NULL is defined as %d\n", NULL );

and a number were outputted, then calloc() alone will not
suffice as a memory allocation function.
Then the following would be required:

char *memory = NULL;

if ( memory != NULL )
    printf( "memory is not set\n" );

Is this your point?

If it is, this contradicts "The C Programming Language -- ANSI C"
by Kernighan and Ritchie on page 102. It says, "...the constant zero
may be assigned to a pointer, and a pointer may be compared with
the constant zero. The symbolic constant NULL is often used in place
of zero as a mnemonic to indicate more clearly that this is a special value
for a pointer."

This implies that NULL always equals zero. Therefore,
it is safe to use calloc() to allocate memory to
structures with pointers and use those pointers in
"if" statements like this:

PERSON *person = (PERSON *)calloc( 1, sizeof( PERSON ) );

if ( !person )
    fprintf( stderr,
             "Memory allocation failed in %s/%s()\n",
             __FILE__, __FUNCTION__ );
    exit( 1 );

if ( person->zip_code )
    printf( "The zip code is populated with %s\n",
            person->zip_code );
    printf( "The zip code is not populated.\n" );

and the behavior will be the not populated statement outputted.

It is my preference to use calloc() alone instead of malloc() with
a bunch of initializations to avoid unnecessary typing.



vox-tech mailing list

LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
O'Reilly and Associates
For numerous book donations.