Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a local variable on the stack.
Embedded C
void func() {
int [1] = 10;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using global or static variable names instead of local.
✗ Incorrect
The variable local_var is declared inside the function and stored on the stack.
2fill in blank
mediumComplete the code to allocate memory dynamically on the heap.
Embedded C
int* ptr = (int*) [1](sizeof(int)); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'new' which is C++ syntax, or 'alloc' which is incorrect.
✗ Incorrect
malloc allocates memory on the heap in C.
3fill in blank
hardFix the error in the code to correctly free heap memory.
Embedded C
int* ptr = malloc(sizeof(int));
// ... use ptr
[1](ptr); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using C++ keywords like 'delete' or incorrect functions like 'dispose'.
✗ Incorrect
free releases memory allocated by malloc on the heap.
4fill in blank
hardFill both blanks to create a stack-allocated array and access its first element.
Embedded C
int arr[[1]]; arr[[2]] = 5;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as the first index or invalid array sizes.
✗ Incorrect
The array size is 10, and the first element index is 0.
5fill in blank
hardFill all three blanks to allocate, assign, and free heap memory correctly.
Embedded C
int* ptr = [1](sizeof(int)); *ptr = [2]; [3](ptr);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using C++ keywords or forgetting to free memory.
✗ Incorrect
Use malloc to allocate, assign value 42, then free to release memory.