Hacker News new | ask | show | jobs
by bch 4519 days ago
The article says two different things about how free() works:

  1. In case of NULL pointer free does no action.
  2. In "double free corruption" section, it says "Could be caused by calling free with pointer, which is [..] NULL pointer"
So: which is it? Otherwise, there's no point in the NULL/assert() dance, you can freely free() with impunity:

  struct foo *a, *b, *c;
  a=NULL; b=NULL; c=NULL;
  a=malloc(sizeof *a);
  b=malloc(sizeof *b);
  c=malloc(sizeof *c);

  if(!(a && b && c)) {free(a); free(b); free(c); return 1;}
2 comments

What the article intended to say is calling free() on the same pointer twice is considered 'double free'. Also an issue with the c++ delete operator.

Calling free() on NULL is a no-op.

Both. free(null) is fine. free(something_already_freed_or_not_returned_by_malloc) (and friends) is undefined behavior.