Multiple choice technology programming languages

What is wrong with the following code? int * x = (int *) malloc(100 * sizeof(int)); x = realloc(x, sizeof(int) * 200);

  1. If realloc fails, then the original memory is lost

  2. Nothing, realloc is guaranteed to succeed (by returning the original pointer)

  3. Nothing, realloc frees the original memory passed to it

  4. None of the above

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

If realloc fails, it returns NULL and the original pointer to allocated memory is lost, causing a memory leak. The correct approach is to use a temporary pointer for realloc, check if it succeeded, then assign back. Realloc does not guarantee success, and option C is wrong because realloc does NOT automatically free the original memory on failure.