0
0
Cprogramming~10 mins

malloc 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 an integer using malloc.

C
int *ptr = (int *)[1](sizeof(int));
Drag options to blanks, or click blank then click option'
Acalloc
Bfree
Csizeof
Dmalloc
Attempts:
3 left
💡 Hint
Common Mistakes
Using free instead of malloc.
Using sizeof as a function to allocate memory.
Using calloc without proper parameters.
2fill in blank
medium

Complete the code to allocate memory for an array of 5 integers.

C
int *arr = (int *)malloc([1] * sizeof(int));
Drag options to blanks, or click blank then click option'
A5
Bint
Cmalloc
Dsizeof(int)
Attempts:
3 left
💡 Hint
Common Mistakes
Using sizeof(int) twice.
Using the type name instead of the number of elements.
Passing malloc as a parameter.
3fill in blank
hard

Fix the error in the code to correctly allocate memory for a float pointer.

C
float *ptr = (float *)malloc(sizeof([1]));
Drag options to blanks, or click blank then click option'
Aint
Bdouble
Cfloat
Dchar
Attempts:
3 left
💡 Hint
Common Mistakes
Using int instead of float inside sizeof.
Using double or char which are different sizes.
Not casting the malloc return.
4fill in blank
hard

Fill both blanks to allocate memory for an array of 10 doubles and check if allocation succeeded.

C
double *arr = (double *)malloc([1] * sizeof([2]));
if (arr == NULL) {
    printf("Memory allocation failed\n");
}
Drag options to blanks, or click blank then click option'
A10
Bfloat
Cdouble
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 5 instead of 10 for the number of elements.
Using float instead of double for the type.
Not checking if malloc returned NULL.
5fill in blank
hard

Fill all three blanks to allocate memory for a char array of 20 elements, initialize it with zeros, and check for success.

C
char *buffer = (char *)[1](20, sizeof([2]));
if (buffer == [3]) {
    printf("Allocation failed\n");
}
Drag options to blanks, or click blank then click option'
Acalloc
Bchar
CNULL
Dmalloc
Attempts:
3 left
💡 Hint
Common Mistakes
Using malloc instead of calloc when zero initialization is needed.
Checking pointer against 0 instead of NULL.
Using wrong type inside sizeof.