← Back to Pointers
Question 457
Free It, Then NULL It
Free It, Then NULL It
Read an integer n. Allocate n ints on the heap and set element i to (i + 1) * 10. Then print exactly five lines: - "First:<element 0>" - "Last:<element n - 1>" - "Is NULL before free: <1 or 0>" - then free the block, set the pointer to NULL, and print "Is NULL after free: <1 or 0>" - then call free on the now-NULL pointer a second time and print "Freed twice safely" For the input: 3 the output is exactly what is shown in Expected Output. Print the NULL comparison, never the address itself. Free the memory you allocate before the program ends — this question checks for memory leaks.
Expected Output:
First: 10 Last: 30 Is NULL before free: 0 Is NULL after free: 1 Freed twice safely
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Pointers
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (p, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.