0
0
Compiler Designknowledge~10 mins

Dynamic memory allocation (heap) in Compiler Design - 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 dynamically on the heap.

Compiler Design
ptr = malloc([1]);
Drag options to blanks, or click blank then click option'
Asizeof(int)
Bint
Cfree
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the type name instead of size.
Using free instead of malloc.
Passing a number without considering the size of the type.
2fill in blank
medium

Complete the code to free the allocated memory correctly.

Compiler Design
free([1]);
Drag options to blanks, or click blank then click option'
Asizeof(ptr)
BNULL
Cmalloc
Dptr
Attempts:
3 left
💡 Hint
Common Mistakes
Passing NULL to free.
Passing the size instead of the pointer.
Calling malloc inside free.
3fill in blank
hard

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

Compiler Design
ptr = malloc(5 [1] sizeof(int));
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication.
Using division or subtraction which are incorrect here.
Forgetting to multiply by the size of the type.
4fill in blank
hard

Fill both blanks to correctly allocate and check memory allocation.

Compiler Design
ptr = malloc([1]);
if (ptr == [2]) {
    // handle error
}
Drag options to blanks, or click blank then click option'
Asizeof(int) * 10
BNULL
C0
Dptr
Attempts:
3 left
💡 Hint
Common Mistakes
Not multiplying by the number of elements.
Checking pointer against 0 instead of NULL.
Using the pointer itself in the condition.
5fill in blank
hard

Fill all three blanks to correctly allocate and initialize an array of 5 integers to zero.

Compiler Design
ptr = [1]([2], [3]);
Drag options to blanks, or click blank then click option'
Acalloc
B5
Csizeof(int)
Dmalloc
Attempts:
3 left
💡 Hint
Common Mistakes
Using malloc, which does not initialize memory.
Confusing the arguments for calloc (num elements vs. total size).
Forgetting sizeof(int) for the element size.