0
0
Embedded Cprogramming~10 mins

Why dynamic memory is risky in embedded in Embedded C - Test Your Understanding

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

Complete the code to declare a pointer for dynamic memory allocation.

Embedded C
int* [1];
Drag options to blanks, or click blank then click option'
Aptr
Bint
Carray
Dmalloc
Attempts:
3 left
💡 Hint
Common Mistakes
Using a type name instead of a variable name.
2fill in blank
medium

Complete the code to allocate memory dynamically for 10 integers.

Embedded C
ptr = (int*) [1](10 * sizeof(int));
Drag options to blanks, or click blank then click option'
Afree
Bmalloc
Csizeof
Dcalloc
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'free' instead of 'malloc'.
3fill in blank
hard

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

Embedded C
if (ptr != NULL) {
    [1](ptr);
}
Drag options to blanks, or click blank then click option'
Amalloc
Bcalloc
Cfree
Drealloc
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'malloc' or 'calloc' instead of 'free'.
4fill in blank
hard

Fill both blanks to check if memory allocation failed and handle it.

Embedded C
ptr = (int*) malloc(sizeof(int) * 5);
if (ptr [1] NULL) {
    [2]("Allocation failed!\n");
}
Drag options to blanks, or click blank then click option'
A==
Bprintf
C!=
Dscanf
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' in the condition.
Using 'scanf' instead of 'printf'.
5fill in blank
hard

Fill all three blanks to check if memory allocation failed, print an error if it did, and initialize the array otherwise.

Embedded C
int* ptr = (int*) malloc(sizeof(int) * 10);
if (ptr [1] NULL) {
    [2]("Memory allocation failed!\n");
} else {
    for (int i = 0; i [3] 10; i++) {
        ptr[i] = i * i;
    }
}
Drag options to blanks, or click blank then click option'
A==
Bprintf
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' for failure check.
Using '>' instead of '<' in loop condition.