Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

What is the size of the malloc ( ) function?


Asked by Aleena Moody on Dec 07, 2021 FAQ



Following is the declaration for malloc () function. size − This is the size of the memory block, in bytes. This function returns a pointer to the allocated memory, or NULL if the request fails. The following example shows the usage of malloc () function.
In addition,
The malloc function returns a pointer to the allocated memory of byte_size. Example: ptr = (int *) malloc (50) When this statement is successfully executed, a memory space of 50 bytes is reserved. The address of the first byte of reserved space is assigned to the pointer ptr of type int.
Likewise, If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item. Always check the return from malloc, even if the amount of memory requested is small. The malloc function allocates a memory block of at least size bytes.
Just so,
The name malloc stands for "memory allocation". The function malloc() reserves a block of memory of specified size and return a pointer of type void which can be casted into pointer of any form. is pointer of cast-type. The malloc() function returns a pointer to an area of memory with size of byte size.
Subsequently,
Syntax of malloc() ptr = (cast-type*) malloc(byte-size) Example. ptr = (int*) malloc(100 * sizeof(float)); This statement allocates 400 bytes of memory. It's because the size of int is 4 bytes. And, the pointer ptr holds the address of the first byte in the allocated memory.