What's the point of malloc(0)?

后端 未结 17 1653
庸人自扰
庸人自扰 2020-11-22 15:40

I just saw this code:

artist = (char *) malloc(0);

...and I was wondering why would one do this?

17条回答
  •  太阳男子
    2020-11-22 15:48

    According to the specifications, malloc(0) will return either "a null pointer or a unique pointer that can be successfully passed to free()".

    This basically lets you allocate nothing, but still pass the "artist" variable to a call to free() without worry. For practical purposes, it's pretty much the same as doing:

    artist = NULL;
    

提交回复
热议问题