What's the point of malloc(0)?

后端 未结 17 1777
庸人自扰
庸人自扰 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:50

    According to Reed Copsey answer and the man page of malloc , I wrote some examples to test. And I found out malloc(0) will always give it a unique value. See my example :

    char *ptr;
    if( (ptr = (char *) malloc(0)) == NULL )
        puts("Got a null pointer");
    else
        puts("Got a valid pointer");
    

    The output will be "Got a valid pointer", which means ptr is not null.

提交回复
热议问题