← Back to Pointers
Question 453
Grow a Heap Array with realloc
Grow a Heap Array with realloc
Read an integer n, then n integers. Allocate room for n ints and store them, then print "Size:<n>". Next read an integer m, grow the SAME block to n + m ints with realloc, read m more integers into the new space, and print "Size:<n + m>". Finally print a line "Values:" followed by every element, each preceded by a single space. For the input: 3 1 2 3 2 4 5 the output is exactly what is shown in Expected Output. The first three values must survive the resize. Free the memory you allocate before the program ends — this question checks for memory leaks.
Expected Output:
Size: 3 Size: 5 Values: 1 2 3 4 5
= 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.