0
0
Cprogramming~10 mins

free 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 free the memory allocated to pointer ptr.

C
free([1]);
Drag options to blanks, or click blank then click option'
A*ptr
B&ptr
Cptr
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the address of the pointer instead of the pointer itself.
Passing a dereferenced pointer instead of the pointer.
2fill in blank
medium

Complete the code to allocate and then free an integer pointer.

C
int *p = malloc(sizeof(int));
if (p != NULL) {
    *p = 10;
    [1](p);
}
Drag options to blanks, or click blank then click option'
Adelete
Bfree
Crelease
Ddispose
Attempts:
3 left
💡 Hint
Common Mistakes
Using C++ keyword delete in C code.
Using non-existent functions like release or dispose.
3fill in blank
hard

Fix the error in the code to properly free the allocated memory.

C
char *buffer = malloc(100);
if (buffer != NULL) {
    // use buffer
    [1](buffer);
}
Drag options to blanks, or click blank then click option'
Afree
Bfree_ptr
Cfree(buffer)
Dfree(buffer);
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the address of the pointer instead of the pointer.
Including parentheses or semicolon inside the blank.
4fill in blank
hard

Fill both blanks to correctly allocate and free memory for an integer array.

C
int *arr = [1](5 * sizeof(int));
if (arr != NULL) {
    // use arr
    [2](arr);
}
Drag options to blanks, or click blank then click option'
Amalloc
Bfree
Ccalloc
Drealloc
Attempts:
3 left
💡 Hint
Common Mistakes
Using calloc instead of malloc.
Using realloc without an existing allocation.
Forgetting to free the allocated memory.
5fill in blank
hard

Fill all three blanks to allocate memory for a struct, assign a value, and then free the memory.

C
typedef struct {
    int id;
} Item;

Item *item = [1](sizeof(Item));
if (item != NULL) {
    item->id = [2];
    [3](item);
}
Drag options to blanks, or click blank then click option'
Amalloc
B42
Cfree
Dcalloc
Attempts:
3 left
💡 Hint
Common Mistakes
Using calloc instead of malloc.
Assigning a pointer instead of an integer to id.
Not freeing the allocated memory.