What's the point of malloc(0)?

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

    To actually answer the question made: there is no reason to do that

    0 讨论(0)
  • 2020-11-22 15:44

    Its actually quite useful, and (obviously IMHO), the allowed behavior of returning a NULL pointer is broken. A dynamic pointer is useful not only for what it points at, but also the fact that it's address is unique. Returning NULL removes that second property. All of the embedded mallocs I program (quite frequently in fact) have this behavior.

    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2020-11-22 15:48

    malloc(0) behaviour is implementation specific. The library can return NULL or have the regular malloc behaviour, with no memory allocated. Whatever it does, it must be documented somewhere.

    Usually, it returns a pointer that is valid and unique but should NOT be dereferenced. Also note that it CAN consume memory even though it did not actually allocate anything.

    It is possible to realloc a non null malloc(0) pointer.

    Having a malloc(0) verbatim is not much use though. It's mostly used when a dynamic allocation is zero byte and you didn't care to validate it.

    0 讨论(0)
  • 2020-11-22 15:48

    Here is the analysis after running with valgrind memory check tool.

    ==16740== Command: ./malloc0
    ==16740==
    p1 = 0x5204040
    ==16740==
    ==16740== HEAP SUMMARY:
    ==16740==     in use at exit: 0 bytes in 0 blocks
    ==16740==   total heap usage: 2 allocs, 2 frees, 1,024 bytes allocated
    ==16740==
    ==16740== All heap blocks were freed -- no leaks are possible
    

    and here's my sample code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main()
    {
       //int i;
       char *p1;
    
       p1 = (char *)malloc(0);
       printf("p1 = %p\n", p1);
    
       free(p1);
    
       return 0;
    
    }
    

    By default 1024 bytes is allocated. If I increase the size of malloc, the allocated bytes will increase by 1025 and so on.

    0 讨论(0)
  • 2020-11-22 15:48

    malloc(0) will return a valid memory address and whose range will depend on the type of pointer which is being allocated memory. Also you can assign values to the memory area but this should be in range with the type of pointer being used. You can also free the allocated memory. I will explain this with an example:

    int *p=NULL;
    p=(int *)malloc(0);
    free(p);
    

    The above code will work fine in a gcc compiler on Linux machine. If you have a 32 bit compiler then you can provide values in the integer range, i.e. -2147483648 to 2147483647. Same applies for characters also. Please note that if type of pointer declared is changed then range of values will change regardless of malloc typecast, i.e.

    unsigned char *p=NULL;
    p =(char *)malloc(0);
    free(p);
    

    p will take a value from 0 to 255 of char since it is declared an unsigned int.

    0 讨论(0)
提交回复
热议问题