Memory Allocation

Rick Schubert rns at se-sd.sandiego.ncr.com
Sat Feb 11 07:35:07 AEST 1989


[Sorry if this is a duplicate; I don't think my original got out.]

My standard 2-day waiting period is up, and no one else has made this
comment, so here goes:

In article <1435 at arctic.nprdc.arpa> snguyen at nprdc.arpa (Son Nguyen) writes:
>	   if ((var = (struct abc *) malloc (sizeof (struct abc))) == NULL)  {
>		printf ("ERROR: Can't allocate memory for 'var'\n");
>		exit (1);
>	   }

That should be:
		fprintf (stderr, "ERROR: Can't allocate memory for 'var'\n");

Error messages should be written to stderr, not stdout (which is where printf
writes); that's what stderr is for.  This is important (on systems that have
pipes) if stdout is piped into another program or (on systems that have
file redirection) if stdout is redirected to a file.  (But it's good to see
that you've checked the return from malloc!)

-- Rick Schubert (rns at se-sd.sandiego.NCR.COM)



More information about the Comp.lang.c mailing list