0
0
Cprogramming~10 mins

calloc function - Interactive Code Practice

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

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

C
int *arr = (int *)[1](10, sizeof(int));
Drag options to blanks, or click blank then click option'
Acalloc
Brealloc
Cfree
Dmalloc
Attempts:
3 left
💡 Hint
Common Mistakes
Using malloc instead of calloc when zero-initialization is needed.
Forgetting to cast the return value of calloc.
2fill in blank
medium

Complete the code to check if calloc failed to allocate memory.

C
if (ptr == [1]) {
    printf("Allocation failed\n");
}
Drag options to blanks, or click blank then click option'
A1
BNULL
C0
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if pointer equals 0 instead of NULL.
Not checking the pointer at all.
3fill in blank
hard

Fix the error in the calloc usage to allocate memory for 5 doubles.

C
double *data = (double *)calloc([1], sizeof(double));
Drag options to blanks, or click blank then click option'
A5
Bsizeof(double)
Csizeof(int)
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the number of elements and size arguments.
Using sizeof(int) instead of sizeof(double).
4fill in blank
hard

Fill both blanks to allocate memory for 8 characters and check if allocation succeeded.

C
char *buffer = (char *)[1]([2], sizeof(char));
if (buffer == NULL) {
    printf("Memory allocation failed\n");
}
Drag options to blanks, or click blank then click option'
Acalloc
Bmalloc
C8
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using malloc instead of calloc when zero-initialization is needed.
Not checking for NULL after allocation.
5fill in blank
hard

Fill all three blanks to allocate memory for 12 floats, check allocation, and free the memory.

C
float *arr = (float *)[1]([2], sizeof(float));
if (arr == [3]) {
    printf("Allocation failed\n");
} else {
    free(arr);
}
Drag options to blanks, or click blank then click option'
Amalloc
B12
CNULL
Dcalloc
Attempts:
3 left
💡 Hint
Common Mistakes
Not freeing allocated memory.
Checking pointer against 0 instead of NULL.