Bird
Raised Fist0
Cprogramming~10 mins

realloc function - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to allocate memory for 10 integers using malloc.

C
#include <stdlib.h>

int *arr = (int *)[1](10 * sizeof(int));
Drag options to blanks, or click blank then click option'
Amalloc
Brealloc
Ccalloc
Dfree
Attempts:
3 left
💡 Hint
Common Mistakes
Using realloc before any memory is allocated.
Using free instead of malloc.
2fill in blank
medium

Complete the code to resize the memory block pointed by arr to hold 20 integers using realloc.

C
arr = (int *)[1](arr, 20 * sizeof(int));
Drag options to blanks, or click blank then click option'
Amalloc
Bcalloc
Crealloc
Dfree
Attempts:
3 left
💡 Hint
Common Mistakes
Using malloc instead of realloc to resize memory.
Not casting the return value of realloc.
3fill in blank
hard

Fix the error in the code to safely resize memory and check for allocation failure.

C
int *temp = (int *)[1](arr, 30 * sizeof(int));
if (temp == NULL) {
    // handle error
} else {
    arr = temp;
}
Drag options to blanks, or click blank then click option'
Arealloc
Bmalloc
Ccalloc
Dfree
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning realloc result directly to original pointer without checking for NULL.
Using free instead of realloc.
4fill in blank
hard

Fill both blanks to create a new array with double the size of the old one using realloc and initialize new elements to zero.

C
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;
}
Drag options to blanks, or click blank then click option'
Arealloc
Bmalloc
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using malloc instead of realloc for resizing.
Initializing new elements with 1 instead of 0.
5fill in blank
hard

Fill all three blanks to safely resize an integer array, check for failure, and update the pointer.

C
int *temp = (int *)[1](arr, [2] * sizeof(int));
if (temp == [3]) {
    // handle allocation failure
} else {
    arr = temp;
}
Drag options to blanks, or click blank then click option'
Amalloc
Brealloc
C20
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Using malloc instead of realloc for resizing.
Checking against 0 instead of NULL.
Not checking for allocation failure before updating pointer.

Practice

(1/5)
1. What does the realloc function do in C programming?
easy
A. It allocates a new memory block without preserving old data.
B. It frees the allocated memory block immediately.
C. It changes the size of a previously allocated memory block, preserving its content.
D. It copies data from one memory block to another.

Solution

  1. Step 1: Understand the purpose of realloc

    The realloc function is used to resize an existing memory block allocated by malloc or calloc.
  2. Step 2: Check data preservation behavior

    realloc preserves the existing data up to the minimum of old and new sizes.
  3. Final Answer:

    It changes the size of a previously allocated memory block, preserving its content. -> Option C
  4. Quick Check:

    realloc resizes memory and keeps data [OK]
Hint: Remember: realloc resizes and keeps old data safe [OK]
Common Mistakes:
  • Thinking realloc frees memory without resizing
  • Assuming realloc always allocates new memory without preserving data
  • Confusing realloc with memcpy or free
2. Which of the following is the correct syntax to resize a memory block pointed by ptr to hold 50 integers using realloc?
easy
A. ptr = realloc(ptr, 50 * sizeof(int));
B. ptr = realloc(sizeof(int), 50 * ptr);
C. ptr = realloc(50, ptr * sizeof(int));
D. ptr = realloc(ptr, 50);

Solution

  1. Step 1: Understand realloc parameters

    realloc takes a pointer and the new size in bytes. To hold 50 integers, size must be 50 times size of int.
  2. Step 2: Check each option's correctness

    ptr = realloc(ptr, 50 * sizeof(int)); correctly multiplies 50 by sizeof(int). Others misuse parameters or sizes.
  3. Final Answer:

    ptr = realloc(ptr, 50 * sizeof(int)); -> Option A
  4. Quick Check:

    Size in bytes = count * sizeof(type) [OK]
Hint: Multiply count by sizeof(type) for realloc size [OK]
Common Mistakes:
  • Passing count instead of bytes as size
  • Swapping parameters in realloc
  • Forgetting sizeof(int) when resizing
3. What will be the output of this code snippet?
int *arr = malloc(3 * sizeof(int));
arr[0] = 1; arr[1] = 2; arr[2] = 3;
arr = realloc(arr, 5 * sizeof(int));
arr[3] = 4; arr[4] = 5;
for(int i = 0; i < 5; i++) printf("%d ", arr[i]);
free(arr);
medium
A. 1 2 3 0 0
B. 1 2 3 4 5
C. Garbage values after 3 elements
D. Runtime error due to invalid access

Solution

  1. Step 1: Analyze memory allocation and initialization

    Initially, 3 integers allocated and set to 1, 2, 3. Then resized to hold 5 integers.
  2. Step 2: Assign new values and print

    After realloc, elements 3 and 4 are assigned 4 and 5. Printing all 5 elements outputs 1 2 3 4 5.
  3. Final Answer:

    1 2 3 4 5 -> Option B
  4. Quick Check:

    realloc preserves old data and new elements assigned [OK]
Hint: realloc keeps old data; new elements must be assigned [OK]
Common Mistakes:
  • Assuming new elements are zeroed automatically
  • Expecting garbage values without assignment
  • Thinking realloc frees old data immediately
4. Identify the error in this code snippet:
int *ptr = malloc(10 * sizeof(int));
ptr = realloc(ptr, 20 * sizeof(int));
if (ptr == NULL) {
    printf("Memory allocation failed\n");
    free(ptr);
}
medium
A. Calling free(ptr) after ptr is NULL does not free the original memory block.
B. Missing check for malloc failure before realloc.
C. Incorrect size calculation in realloc.
D. No error; code is correct.

Solution

  1. Step 1: Understand realloc return and NULL check

    If realloc returns NULL, original pointer is lost if not saved separately, and ptr becomes NULL.
  2. Step 2: Analyze free(ptr) after NULL assignment

    Calling free(ptr) after ptr is NULL does not free the original memory block, leading to memory leak.
  3. Final Answer:

    Calling free(ptr) after ptr is NULL does not free the original memory block. -> Option A
  4. Quick Check:

    Check realloc result before overwriting pointer [OK]
Hint: Save realloc result before overwriting pointer [OK]
Common Mistakes:
  • Not checking realloc return before assignment
  • Freeing NULL pointer thinking it frees old memory
  • Ignoring malloc failure before realloc
5. You have a dynamic array of integers allocated with malloc. You want to safely double its size using realloc without losing data or causing memory leaks. Which approach is correct?
hard
A. ptr = malloc(2 * current_size); memcpy(ptr, old_ptr, current_size); free(old_ptr);
B. ptr = realloc(ptr, 2 * current_size); // directly assign realloc result
C. free(ptr); ptr = malloc(2 * current_size); // allocate new memory after freeing
D. int *temp = realloc(ptr, 2 * current_size); if (temp) ptr = temp; else handle error;

Solution

  1. Step 1: Understand safe realloc usage

    Directly assigning realloc result to original pointer risks losing original memory if realloc fails.
  2. Step 2: Use temporary pointer to check realloc success

    Assign realloc to temp pointer, check if not NULL, then update original pointer. Else handle error safely.
  3. Final Answer:

    int *temp = realloc(ptr, 2 * current_size); if (temp) ptr = temp; else handle error; -> Option D
  4. Quick Check:

    Use temp pointer to avoid memory leaks on realloc failure [OK]
Hint: Use temp pointer for realloc to avoid losing original memory [OK]
Common Mistakes:
  • Overwriting pointer before checking realloc result
  • Freeing pointer before realloc causing data loss
  • Not handling realloc failure properly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard