Freeing C pointers declared in the middle of a block (seeking documentation on this) -
where behaviour documented (if @ all)?
when declare pointer in c in middle of block, in wrong state (pointing unusable memory) , cannot use standard if (a) free(a)
freeing it.
the simplest program comes mind is
#include <stdlib.h> int main(int argc, char *argv[]){ if(argc > 1) goto end; char *a = null; = calloc(1,1); end: if(a) free(a); }
run program without parameters , works ok, if run @ least 1 parameter, break follows: suprisingly (to me), if compile clang, may work (on os x does, on netbsd not). if gcc, returns a
malloc: *** error object 0x7fff5fc01052: pointer being freed not allocated
notice same program declaration @ head of block correct.
edit: notice question documentation. realize doing describe unsafe have found no place explicitly shown.
the "pattern"
if(a) free(a);
is not standard, or @ least shouldn't be. it's safe pass null
free()
, if
adds nothing.
i expect value of a
undefined (not null
) if jump past initialization, makes perfect sense me. hairy code, don't this.
it argued existance of goto
, label imply scope isn't implemented, , there's no reason @ free(a);
statement after label (outside "invisible" scope a
defined).
Comments
Post a Comment