if (pointer) command

Brandon Allbery allbery at ncoast.UUCP
Sat Sep 6 08:18:10 AEST 1986


Quoted from <1170 at ttrdc.UUCP> ["Re: strdup"], by levy at ttrdc.UUCP (Daniel R. Levy)...
+---------------
| In article <241 at bsdpkh.UUCP>, heff at bsdpkh.UUCP (Paul K Heffner) writes:
| 
| >> /* hjb 08/25/86 */
| >> char * strdup(s)
| >> char *s;
| >> {
| >> 	char *p;
| >> 	extern char *malloc();
| >> 	if(p=malloc(strlen(s)+1)) strcpy(p,s);
| >> 	return p;
| >> }
| 
| >The above code doesn't return a null if malloc fails
| 
| It DOESN'T???  Gee, then maybe you have a busted malloc().  If malloc()
| fails, it should return the null pointer.  That is what is being tested in
| the clause 'if(p=malloc(strlen(s)+1))'.  As so many people have expounded
| in net.lang.c:  a null pointer is semantically the same as zero in the C
| language (except for sizeof(), should pointers exist of sizes other than
| sizeof(int)).
+---------------

But if a pointer is 4 bits and an int is 2 bits (some 68000 implementations),
you'll get spurious failures if the pointer returned by malloc is a multiple
of 0x10000!

++Brandon
-- 
  ---------------- /--/	Brandon S. Allbery		UUCP: decvax!cwruecmp!
 /              / /|\/	Tridelta Industries, Inc.        ncoast!tdi2!brandon
----    -------- /-++	7350 Corporate Blvd.		PHONE: +1 216 974 9210
   /   / /---,  ----	Mentor, Ohio 44060		SYSOP: UNaXcess/ncoast
  /   / /    / /  /	     -- HOME --			 (216) 781-6201 24 hrs.
 /   / /    / /  /	6615 Center St. Apt. A1-105	ARPA:  ncoast!allbery%
----  -----~ ----	Mentor, Ohio 44060-4101		 case.CSNET at csnet-relay



More information about the Comp.unix mailing list