0
0
Cprogramming~10 mins

Memory allocation flow - 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 pointer.

C
int *ptr = (int *)[1](sizeof(int));
Drag options to blanks, or click blank then click option'
Afree
Bmalloc
Csizeof
Dcalloc
Attempts:
3 left
💡 Hint
Common Mistakes
Using free instead of malloc causes errors because free releases memory.
Using sizeof alone does not allocate memory.
2fill in blank
medium

Complete the code to release the allocated memory.

C
free([1]);
Drag options to blanks, or click blank then click option'
Aptr
BNULL
Cmalloc
Dsizeof
Attempts:
3 left
💡 Hint
Common Mistakes
Passing sizeof or malloc to free causes runtime errors.
Passing NULL to free does nothing but is not useful here.
3fill in blank
hard

Fix the error in the code to allocate memory for 5 integers.

C
int *arr = (int *)malloc([1] * sizeof(int));
Drag options to blanks, or click blank then click option'
Asizeof(arr)
Bsizeof(int *)
C5
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using sizeof(arr) gives size of pointer, not the number of elements.
Using 0 allocates no memory.
4fill in blank
hard

Fill both blanks to create a loop that initializes the allocated array to zero.

C
for (int [1] = 0; [2] < 5; [1]++) {
    arr[[1]] = 0;
}
Drag options to blanks, or click blank then click option'
Ai
Bj
Ck
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the loop causes errors.
Using a variable not declared causes compilation errors.
5fill in blank
hard

Fill all three blanks to safely allocate memory and check if allocation succeeded.

C
int *ptr = (int *)[1](sizeof(int) * [2]);
if ([3] == NULL) {
    printf("Memory allocation failed\n");
    return 1;
}
Drag options to blanks, or click blank then click option'
Amalloc
B10
Cptr
Dfree
Attempts:
3 left
💡 Hint
Common Mistakes
Using free instead of malloc causes errors.
Not checking if pointer is NULL can cause crashes.
Using wrong variable name in the if condition causes errors.