Classwork 26
Logistics
- Due: Monday, April 22th AoE.
- Submission instructions: make sure that the required file (
realloc.c
) is in yourcsci112_spring2024/classwork/week15/mon/
directory. - Deadline reminder: after the deadline passes, you cannot earn any points for this assignment.
Learning outcomes
- Practice using
realloc
- Practice checking whether a memory allocation was successful
- Practice freeing memory
Assignment
- Log in to the server.
- From your home directory, navigate to
csci112_spring2024/classwork/week15/mon/
, creating directoriesweek15
andmon
if necessary. - Copy the file
/public/classwork/week15/mon/realloc.c
into your directory.
Fill in the three TODO
items so that the program attempts to allocate an
array of ints on the heap, and if successful, then attempts to reallocate the same data to
a different sized array of ints on the heap. Be sure to free
memory as
needed! You may need to think about how to ensure that all memory is freed in
the event that your realloc
call is unsuccessful.
Here are a few sample runs:
[g73x595@csci112 mon]$ gcc realloc.c -Wall
[g73x595@csci112 mon]$ ./a.out
How many integers would you like to store? 10
Allocating space for 10 ints
How many do you want to store now? 5000
Allocating space for 5000 ints
[g73x595@csci112 mon]$ ./a.out
How many integers would you like to store? 1000
Allocating space for 1000 ints
How many do you want to store now? 1000000000000
Allocating space for 1000000000000 ints
Not enough memory
[g73x595@csci112 mon]$ ./a.out
How many integers would you like to store? 100000000000
Allocating space for 100000000000 ints
Not enough memory
Autograder
You can run the autograder by running
/public/classwork/week15/mon/autograder.sh
The file autograder.txt
will be created with your score.
Grading turnaround
The autograder will be run at 6am to calculate final scores. Scores will be uploaded to D2L by the next class period.