Maybe it would be better to return -ENOMEM... or throw bad_alloc