0
0
Cprogramming~10 mins

Memory leak concepts - 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'
Amalloc
Bfree
Cprintf
Dsizeof
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'free' instead of 'malloc' to allocate memory.
Using 'sizeof' as a function to 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'
Amalloc
Bptr
Csizeof
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'malloc' or 'sizeof' instead of the pointer.
Not freeing allocated memory causing leaks.
3fill in blank
hard

Fix the error in the code to avoid memory leak by freeing the allocated memory.

C
int *arr = (int *)malloc(5 * sizeof(int));
// Missing [1] here
return 0;
Drag options to blanks, or click blank then click option'
Afree(arr);
Bmalloc(arr);
Csizeof(arr);
Darr = NULL;
Attempts:
3 left
💡 Hint
Common Mistakes
Not calling free causing memory leaks.
Calling malloc instead of free to release memory.
4fill in blank
hard

Fill both blanks to correctly allocate and free memory for a character array.

C
char *buffer = (char *)[1](100 * sizeof(char));
// Use the buffer
[2](buffer);
Drag options to blanks, or click blank then click option'
Amalloc
Bfree
Csizeof
Dprintf
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sizeof' or 'printf' instead of 'malloc' or 'free'.
Forgetting to free allocated memory.
5fill in blank
hard

Fill all three blanks to create a memory leak by allocating memory without freeing it.

C
int *data = (int *)[1](10 * [2](int));
// Forgot to [3] data, causing a memory leak.
Drag options to blanks, or click blank then click option'
Amalloc
Bsizeof
Cfree
Dprintf
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to free allocated memory.
Using wrong functions for allocation or size.