How I return the size of the pointer that I have allocate with malloc?

前端 未结 4 1943
醉酒成梦
醉酒成梦 2021-01-15 03:30

See this example!



int main( int argc, char ** argv )
{
    int *ptr = malloc(100 * sizeof (int));

    printf(\"sizeof(array) is %d bytes\\n\", sizeof(ptr)         


        
4条回答
  •  走了就别回头了
    2021-01-15 04:10

    You cannot print the size of the memory block you received. Either malloc allocates all the memory you requested or it does not (and returns NULL).

    The sizeof() operator does what you request: it tells you the size of the pointer - and the pointer itself occupies 4 bytes in memory.

提交回复
热议问题