Assignment 7 — Memory you ask for
Four programs whose functions hand back memory they asked malloc for: a string with a letter taken out, two strings joined into one, an array made bigger, and numbers read in when only the input says how many.
Due Oct 21, 2026
In Class 15, make_range handed back an array that outlived it, because malloc gave it memory that belongs to no function. Each function here does the same: it asks for memory, fills it, and hands back the address, and main gives the memory back with free when it is done with it. Still no [ or ] in any function but main: walk with a pointer.
The reading for the week is [DIS] 2.4 — Dynamic Memory Allocation and [DIS] 2.6.2 — Dynamically Allocating Strings. Quiz 6 on Tuesday is on it.
You will hand in five files: without.c, join.c, grow.c, stats.c and notes.txt — and longest.c and readall.c, if you do the optional steps 6 and 7.
Every program compiles with gcc -Wall and no warnings. Every function you write gets the design recipe’s one-line comment above it. Every malloc is checked for NULL, with an assert or the reading’s if. Every program gets at least one assert of your own, beside the ones given, and frees every array it asked for. malloc and free need #include <stdlib.h>; strlen and strcmp need #include <string.h>.
without.c— a letter taken out, into new memory. Writechar * without(char * s, char c), which hands back a new string holdingswith everyctaken out;sitself is left as it was. Walkstwice: once to count what stays, so you know how many bytes to ask for, and once to copy. Yourmain:int main() { char * t = without("a b c", ' '); assert(strcmp(t, "abc") == 0); free(t); t = without("banana", 'a'); assert(strcmp(t, "bnn") == 0); free(t); t = without("", 'x'); assert(strcmp(t, "") == 0); free(t); printf("all examples passed\n"); }strcmp, from the reading, returns0when two strings hold the same characters. Done when it printsall examples passed.join.c— two strings, into one new one. Writechar * join(char * a, char * b), which hands back a new string holding the characters ofaand then those ofb. Neitheranorbchanges. Yourmain:int main() { char * t = join("point", "er"); assert(strcmp(t, "pointer") == 0); free(t); t = join("", "er"); assert(strcmp(t, "er") == 0); free(t); t = join("point", ""); assert(strcmp(t, "point") == 0); free(t); printf("all examples passed\n"); }Done when it prints
all examples passed. Count the bytes before you ask for them: two lengths, and one more.grow.c— an array made bigger. Writeint * grow(int * a, int n, int more), which hands back a new array ofn + moreelements: thenvalues ofa, then zeros. Yourmain:#define SIZE 3 int main() { int a[SIZE] = {1, 2, 3}; int * b = grow(a, SIZE, 2); assert(b[0] == 1); assert(b[2] == 3); assert(b[3] == 0); assert(b[4] == 0); int * c = grow(b, SIZE + 2, 1); free(b); assert(c[2] == 3); assert(c[5] == 0); free(c); printf("all examples passed\n"); }Done when it prints
all examples passed. The first call grows an arraymaindeclared; the second grows one that came frommalloc.growcannot tell the two apart, and does not need to: each arrives as an address. Notice which arraysmainfrees, and which one it does not.stats.c— as many numbers as the input says. Writeint * read_numbers(int n), which reads the nextnnumbers from the input withscanfinto a new array and hands it back, andint largest(int * a, int n), which returns the largest of thenvalues ata. Yourmainreads how many numbers there are, then the numbers:int main() { int n; scanf("%d", &n); int * a = read_numbers(n); int sum = 0; for (int * p = a; p < a + n; p++) { sum = sum + *p; } printf("%d numbers, sum %d, largest %d\n", n, sum, largest(a, n)); free(a); }Make a file
numbers.txtholding5on its first line and4 8 15 16 23on its second, then run./stats < numbers.txt. Done when it prints5 numbers, sum 66, largest 23. This is the program no#define SIZEcould write: how many numbers there are is not known until it runs.notes.txt— three short answers.- In class,
make_rangecould not hand backawhenawas declaredint a[SIZE], andgcc -Wallsaid so. Whose memory was that array, and what is different about the memorymallochands out? join("point", "er")asksmallocfor 8 bytes, not 7. What is the eighth byte for, and what does C promise will happen if you ask for 7?- After
free(t), the variabletstill holds the address it held before. What does the reading say to do withtnext, and why?
- In class,
longest.c— optional: the longest word. This step and the next are optional and not needed for full marks. A6’s optional step counted the words in a string. Writechar * longest_word(char * s), which hands back a new string holding the longest word ins, a word being a run of characters that are not spaces; whenshas no words, the new string is empty. Find the word first — where it starts, and how long it is — and only then ask for the memory and copy. Yourmain:int main() { char * t = longest_word("a pointer walks the string"); assert(strcmp(t, "pointer") == 0); free(t); t = longest_word(" spaces around "); assert(strcmp(t, "spaces") == 0); free(t); t = longest_word(""); assert(strcmp(t, "") == 0); free(t); printf("all examples passed\n"); }Done when it prints
all examples passed.readall.c— optional: as many numbers as there are. Step 4 read its count first. This program reads every number in the input, however many, with no count anywhere: the array grows as the numbers come. Writeint * read_all(int * count), which hands back a new array holding every number in the input and stores how many there were in*count— the array as the return value and the count through a pointer, the way A5’sdividehanded back two answers. Yourmain:int main() { int n; int * a = read_all(&n); printf("%d numbers, largest %d\n", n, largest(a, n)); free(a); }with
largestwritten as in step 4. The design, in order:read_allkeeps two numbers:capacity, how many values the array has room for, andcount, how many are in it so far. The array starts with room forSTARTvalues, a#defineof4.- The reading loop is
while (scanf("%d", &x) == 1):scanfreturns how many values it read, and something else once the input has run out. - Before storing a value, check whether the array is full:
countequal tocapacity. When it is, make a new array with twice the room, copy thecountvalues over, free the old array, and pointaat the new one;capacitydoubles. Write the copy as its own function,int * copy_into(int * a, int n, int size): a new array ofsizeelements holding thenvalues ofa. - Store the value at
*(a + count), andcountgoes up by one. - At the end,
*countgets the count, and the array is the return value.
A worked example, with nine numbers in the input: the array starts with room for 4. The fifth number finds it full, so a new array of 8 is made, 4 values are copied, and the old one is freed. The ninth number finds that one full too: a new array of 16, 8 copied, the second one freed. The loop ends with
count9 andcapacity16, after three calls tomallocand two tofree; the thirdfreeismain’s.Make a file of a hundred numbers with
seq 1 100 > many.txt, then run./readall < many.txt. Done when it prints100 numbers, largest 100, and./readall < numbers.txtfrom step 4 prints6 numbers, largest 23— six, because to this program the5on the first line is one more number.