0
0
C++programming~10 mins

Memory allocation and deallocation in C++ - 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 using new.

C++
int* ptr = [1] int;
Drag options to blanks, or click blank then click option'
Amalloc
Bnew
Calloc
Dcreate
Attempts:
3 left
💡 Hint
Common Mistakes
Using malloc instead of new in C++.
Forgetting to use new keyword.
2fill in blank
medium

Complete the code to deallocate memory allocated for an integer pointer.

C++
delete [1];
Drag options to blanks, or click blank then click option'
A*ptr
Bptr()
Cptr
D&ptr
Attempts:
3 left
💡 Hint
Common Mistakes
Using *ptr instead of ptr in delete.
Using ptr() which is invalid syntax.
3fill in blank
hard

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

C++
int* arr = [1] int[5];
Drag options to blanks, or click blank then click option'
Anew
Bmalloc
Calloc
Dnew[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using malloc instead of new.
Writing new[] without the type.
4fill in blank
hard

Fill both blanks to correctly deallocate the array allocated with new.

C++
delete[1] [2];
Drag options to blanks, or click blank then click option'
A[]
B()
Cptr
Dptr[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using delete without brackets for arrays.
Using parentheses instead of brackets.
5fill in blank
hard

Fill all three blanks to allocate memory for a double, assign a value, and then deallocate it.

C++
double* [1] = [2] double;
*[1] = 3.14;
delete [3];
[1] = nullptr;
Drag options to blanks, or click blank then click option'
Aptr
Bnew
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Forgetting to delete the allocated memory.