Complete the code to allocate memory for 10 integers using malloc.
#include <stdlib.h> int *arr = (int *)[1](10 * sizeof(int));
The malloc function allocates memory without initializing it. Here, it allocates space for 10 integers.
Complete the code to resize the memory block pointed by arr to hold 20 integers using realloc.
arr = (int *)[1](arr, 20 * sizeof(int));
The realloc function changes the size of the memory block pointed to by arr. It can increase or decrease the size.
Fix the error in the code to safely resize memory and check for allocation failure.
int *temp = (int *)[1](arr, 30 * sizeof(int)); if (temp == NULL) { // handle error } else { arr = temp; }
Use realloc to resize the memory block. Always assign the result to a temporary pointer to avoid losing the original pointer if allocation fails.
Fill both blanks to create a new array with double the size of the old one using realloc and initialize new elements to zero.
int old_size = 10; int new_size = old_size * 2; int *temp = (int *)[1](arr, new_size * sizeof(int)); if (temp != NULL) { for (int i = old_size; i < new_size; i++) { temp[i] = [2]; } arr = temp; }
realloc resizes the array. The new elements from old_size to new_size are set to zero to initialize them.
Fill all three blanks to safely resize an integer array, check for failure, and update the pointer.
int *temp = (int *)[1](arr, [2] * sizeof(int)); if (temp == [3]) { // handle allocation failure } else { arr = temp; }
Use realloc to resize the array to hold 20 integers. Check if the returned pointer is NULL to detect failure before updating the original pointer.