orangeduck / tgc

A Tiny Garbage Collector for C
Other
968 stars 64 forks source link

Need free the old memory if realloc failed. #4

Closed loggerhead closed 8 years ago

loggerhead commented 8 years ago

The old memory should freed in tgc_realloc because:

For realloc(), the input pointer is still valid if reallocation failed.

  void *qtr = realloc(ptr, size);

  if (qtr == NULL) {
    // free(ptr);
    tgc_rem(gc, ptr);
    return qtr;
  }
orangeduck commented 8 years ago

I think it should be up to the caller of tgc_realloc to free the old pointer. Essentially tgc_realloc should match the semantics of realloc.

loggerhead commented 8 years ago

Oh, I don't consider this point, you are right 😄