Complete the code to allocate memory dynamically on the heap.
ptr = malloc([1]);The malloc function requires the size in bytes to allocate memory on the heap. Using sizeof(int) gives the correct size for an integer.
Complete the code to free the allocated memory correctly.
free([1]);The free function releases the memory pointed to by ptr back to the heap.
Fix the error in the code to allocate memory for an array of 5 integers.
ptr = malloc(5 [1] sizeof(int));
To allocate memory for 5 integers, multiply 5 by the size of an integer using the * operator.
Fill both blanks to correctly allocate and check memory allocation.
ptr = malloc([1]); if (ptr == [2]) { // handle error }
Memory is allocated for 10 integers. The pointer is checked against NULL to detect allocation failure.
Fill all three blanks to correctly allocate and initialize an array of 5 integers to zero.
ptr = [1]([2], [3]);
calloc allocates memory for an array of 5 integers and initializes all bytes to zero. It takes the number of elements and the size of each element.