Exercise: Fix the Memory Bug
Question
The following program attempts to return an array from a function. However, the program behaves incorrectly because the array was created on the stack, and its memory becomes invalid after the function returns.
Your task is to fix the program so that it correctly returns the array. Fix the function so that:
The array is allocated on the heap instead of the stack.
The program correctly prints the values.
The allocated memory is properly freed in
main().
Expected output:
0 2 4 6 8
Exercise: Fix the Memory Bug
Question
The following program attempts to return an array from a function. However, the program behaves incorrectly because the array was created on the stack, and its memory becomes invalid after the function returns.
Your task is to fix the program so that it correctly returns the array. Fix the function so that:
The array is allocated on the heap instead of the stack.
The program correctly prints the values.
The allocated memory is properly freed in
main().
Expected output:
0 2 4 6 8