Oh noooooo!!

John Hascall hascall at atanasoff.cs.iastate.edu
Sat Sep 9 00:24:27 AEST 1989


In article <9511 at chinet.chi.il.us> john at chinet.chi.il.us (John Mundt) writes:
}In article <34566 at apple.Apple.COM> ftanaka at Apple.COM (Forrest Tanaka) writes:
}>I've been using gotos regularly in my C code for quite a few months--in one
 
}Pathetic justifications deleted and sample of code follows.
 
 
}char * SomeFunction () {
}    char *Block0, *Block1, *Block2;
 
}    Block0 = Block1 = Block2 = null;
 
}    if ((Block0 = AllocateMemory ()) != null
}    	&& (Block1 = AllocateMemory ()) !=  null
}	&& (Block2 = AllocateMemory ()) !=  null)
}    return Block0;
 
}    if (Block0 != null)
}        DeallocateMemory (Block0);
}    if (Block1 != null)
}        DeallocateMemory (Block1);
}    if (block2 != null)
}        DeallocateMemory (Block2);
}    return null;
}    }

   The code:

       if (Block2 != null)
	   DeallocateMemory (Block2);

   is useless, if Block2 != null you have already returned above!
   How about:

      #define ALLOC(ptr, type) ((ptr = (type*)malloc(sizeof(type))) != NULL)

	 ...
      
      if (ALLOC(B0, FOO)) {
              if (ALLOC(B1, BAR)) {
                      if (ALLOC(B2, BAZ)) {
                              /* all alloced ok, do something with it */
                              return (B0);
                      }
                      FREE(B1);
              }
              FREE(B0);
      }
      return (NULL);      /* couldn't alloc something */
  
  John Hascall



More information about the Comp.lang.c mailing list