Which header file must be included to use dynamic memory allocation functions in C?
Answer: A
stdlib.h contains declarations for malloc(), calloc(), realloc(), and free() functions.
Q.251Easy
What does free() function do in C?
Answer: B
free() is used to deallocate memory that was previously allocated using malloc(), calloc(), or realloc().
Q.252Easy
What is the primary difference between malloc() and calloc()?
Answer: B
calloc() allocates memory and initializes all bytes to zero, while malloc() leaves the allocated memory uninitialized.
Q.253Easy
Which of the following correctly allocates memory for an array of 5 integers and initializes to zero?
Answer: B
calloc(5, sizeof(int)) allocates memory for 5 integers and initializes all to zero.
Q.254Easy
Which function in C is used to allocate memory dynamically at runtime?
Answer: A
malloc() is the standard C library function for dynamic memory allocation. It takes the number of bytes to allocate as an argument and returns a pointer to the allocated memory.
Q.255Easy
Which header file must be included to use malloc() and free() functions?
Answer: A
<stdlib.h> is the standard header file containing declarations for malloc(), calloc(), realloc(), and free() functions.
Q.256Easy
What happens when malloc() fails to allocate memory?
Answer: A
When malloc() cannot allocate the requested memory, it returns NULL. The programmer must check for NULL before using the pointer to avoid undefined behavior.
Q.257Easy
What is the result of calling free() on a NULL pointer?
Answer: A
Calling free(NULL) is safe in C and does nothing. This is by design, allowing you to write code like free(ptr) without checking if ptr is NULL first.
Q.258Easy
How many bytes are allocated by malloc(sizeof(int) * 10) on a system where int is 4 bytes?