I use to use the following memory allocation scheme:
        #define NEW(t) ((t *)(malloc (sizeof (t))))
        
Bad idea.
As already noted, if you forgot to #include <stdlib.h>,
the cast will prevent the warning message you would normally have got.
It's also a bad idea because you have to keep mentioning the type
over and over and over, and if the variable type changes, the code will
break.

Better idea:
    #define new(var) (var) = emalloc(sizeof *(var))
    #define new_array(var, n) (var) = emalloc((n) * sizeof *(var))
where emalloc() is a function just like malloc() but which catches
errors.

Then
    t *var;
    new(var);
    new_array(var, n+1);
        
        t *var = NEW (t)   /* GOOD */
        x *var = NEW (t)   /* BAD, compiler complains about */
        
        x *var = malloc (sizeof t) does not catch this kind of errors
        
which amongst other things is why var = malloc(sizeof *var) is better.
_______________________________________________
splint-discuss mailing list
[EMAIL PROTECTED]
http://www.splint.org/mailman/listinfo/splint-discuss

Reply via email to