Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!sharkey!atanasoff!hascall From: hascall@atanasoff.cs.iastate.edu (John Hascall) Newsgroups: comp.lang.c Subject: Re: use of if (!cptr) and if (cptr), where cptr is a * Keywords: * != int Message-ID: <1225@atanasoff.cs.iastate.edu> Date: 18 Jul 89 15:11:51 GMT References: <10099@mpx2.mpx.com> Reply-To: hascall@atanasoff.cs.iastate.edu.UUCP (John Hascall) Organization: Iowa State Univ. Computation Center Lines: 60 In article <10099@mpx2.mpx.com> erik@mpx2.mpx.com (Erik Murrey) writes: >I'm curious how the gods feel about testing pointers in the following >way (by example): Well, I'm probably not even a minor diety (yet :-) but here's an example of what I use: -------------------------- macro.h ------------------------------------------ #define ALLOC(type,count) \ ((type*)malloc(count*sizeof(type))) #define FATAL(string,val) \ return(fprintf(stderr, "[%4d] 0x0%x <- %s", __LINE__, val, string), val) #define ALLOCATE(ptr,type,count) \ if ((ptr=ALLOC(type,count)) == NULL) FATAL("alloc(ptr, type, count)", NULL) #define SYSCALL(function) \ if (((syscall_status = function)&1) == 0) FATAL("function", syscall_status) static int syscall_status; ---------------------------- foo.c ----------------------------------------- #include #include #include #include "macro.h" typedef struct foo { int bar; int baz; } FOO; main(argc,argv) int argc; char *argv[]; { $DESCRIPTOR( device, "ZZA0:"); unsigned short channel; FOO *cptr; int n_foo; n_foo = atoi(argv[1]); ALLOCATE(cptr, FOO, n_foo); SYSCALL(sys$assign(&device, &channel, 0, 0)); } ------------------------------------------------------------------- The SYSCALL macro really has nothing to do with memory allocation, it just happens to be in my "macro.h" file with the others, (it is a VMS-ism to boot), so I thought I'd through it out anyway. I wish I could come up with better names than "ALLOC" and "ALLOCATE" -- they are *too* similar. John Hascall / ISU Comp Center / Ames IA