[Discuss] Casting the return value of malloc bad?

Daniel M. German dmgerman at uvic.ca
Sun Jul 16 14:58:07 PDT 2006


I guess everybody has different "techniques". This is the one that I
currently used, developed during my years as an IBMer:

int returnValue = -1;
MyType *p = NULL;

do {
    /* allocate memory and use a debug library to detect leaks */
    p = malloc(sizeof(*p));
    if (p == NULL) 
       goto error;

    err = 0;
} while(whatever);

  returnvalue = 0;


error:
  //clean up the function.. 
  // release memory.. etc
  if (p != NULL) free(p);

  return returnValue;

}



--
Daniel M. German                  "You could wind up belivin' 
                                   that paradise is nothin'
                                    more than a feelin'
   Marillion ->                    that goes on in your mind."
http://turingmachine.org/
http://silvernegative.com/
dmg (at) uvic (dot) ca
replace (at) with @ and (dot) with .

 


More information about the Discuss mailing list