I think you should forgo the malloc/getsize/realloc dance and just getsize, then create a unique_ptr. Works around the silliness of this allocation strategy, and gives automatic deletion at end of scope.