0
0
Cprogramming~10 mins

Why dynamic memory is needed - Test Your Understanding

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

Complete the code to allocate memory dynamically for an integer.

C
#include <stdlib.h>

int main() {
    int *ptr = (int*)[1](sizeof(int));
    if (ptr == NULL) {
        return 1;
    }
    *ptr = 10;
    free(ptr);
    return 0;
}
Drag options to blanks, or click blank then click option'
Amalloc
Bprintf
Csizeof
Dfree
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sizeof' instead of a memory allocation function.
Forgetting to cast the return value of malloc.
Using 'free' instead of 'malloc' for allocation.
2fill in blank
medium

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

C
#include <stdlib.h>

int main() {
    int *arr = (int*)malloc([1] * sizeof(int));
    if (arr == NULL) {
        return 1;
    }
    free(arr);
    return 0;
}
Drag options to blanks, or click blank then click option'
A10
B0
Csizeof(arr)
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using the size of the pointer instead of the number of elements.
Allocating zero bytes.
Using an incorrect number for the array size.
3fill in blank
hard

Fix the error in the code to properly allocate and free memory for a float variable.

C
#include <stdlib.h>

int main() {
    float *ptr = (float*)[1](sizeof(float));
    if (ptr == NULL) {
        return 1;
    }
    *ptr = 3.14f;
    free(ptr);
    return 0;
}
Drag options to blanks, or click blank then click option'
Amalloc
Bfree
Csizeof
Dcalloc
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'free' instead of 'malloc' for allocation.
Confusing 'sizeof' with allocation functions.
Using 'calloc' without initializing properly.
4fill in blank
hard

Fill both blanks to allocate memory for 10 doubles and check if allocation was successful.

C
#include <stdlib.h>
#include <stdio.h>

int main() {
    double *data = (double*)[1](10 * sizeof(double));
    if (data [2] NULL) {
        printf("Allocation failed\n");
        return 1;
    }
    free(data);
    return 0;
}
Drag options to blanks, or click blank then click option'
Amalloc
B==
C!=
Dfree
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'free' instead of 'malloc' for allocation.
Checking if pointer equals NULL instead of not equals.
Using '==' instead of '!=' in the condition.
5fill in blank
hard

Fill all three blanks to create a dynamic array of chars, assign a value, and free the memory.

C
#include <stdlib.h>
#include <stdio.h>

int main() {
    char *buffer = (char*)[1](20 * sizeof(char));
    if (buffer == NULL) {
        return 1;
    }
    buffer[0] = [2];
    printf("First char: %c\n", buffer[0]);
    [3](buffer);
    return 0;
}
Drag options to blanks, or click blank then click option'
Amalloc
B'A'
Cfree
Dcalloc
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'calloc' instead of 'malloc' without explanation.
Assigning a string instead of a character.
Forgetting to free the allocated memory.