Difference between initializing a string with (char *)malloc(0) and NULL

前端 未结 3 1972
Happy的楠姐
Happy的楠姐 2021-01-24 14:52

Why allocating a 0 size char block works in this case? But if I write char *string = NULL; it won\'t work.

I\'m using Visual Studio.



        
3条回答
  •  庸人自扰
    2021-01-24 15:41

    malloc definition:

    Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.

    The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.

    If size is zero, the return value depends on the particular library implementation (it may or may not be a null pointer), but the returned pointer shall not be dereferenced.

    Taken from here and found this related question.

提交回复
热议问题