use of if (!cptr) and if (cptr), where cptr is a *
John Hascall
hascall at atanasoff.cs.iastate.edu
Wed Jul 19 01:11:51 AEST 1989
In article <10099 at mpx2.mpx.com> erik at 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 <descrip.h>
#include <stdio.h>
#include <stdlib.h>
#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
More information about the Comp.lang.c
mailing list