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>.

  1. without.c — a letter taken out, into new memory. Write char * without(char * s, char c), which hands back a new string holding s with every c taken out; s itself is left as it was. Walk s twice: once to count what stays, so you know how many bytes to ask for, and once to copy. Your main:

    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, returns 0 when two strings hold the same characters. Done when it prints all examples passed.

  2. join.c — two strings, into one new one. Write char * join(char * a, char * b), which hands back a new string holding the characters of a and then those of b. Neither a nor b changes. Your main:

    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.

  3. grow.c — an array made bigger. Write int * grow(int * a, int n, int more), which hands back a new array of n + more elements: the n values of a, then zeros. Your main:

    #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 array main declared; the second grows one that came from malloc. grow cannot tell the two apart, and does not need to: each arrives as an address. Notice which arrays main frees, and which one it does not.

  4. stats.c — as many numbers as the input says. Write int * read_numbers(int n), which reads the next n numbers from the input with scanf into a new array and hands it back, and int largest(int * a, int n), which returns the largest of the n values at a. Your main reads 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.txt holding 5 on its first line and 4 8 15 16 23 on its second, then run ./stats < numbers.txt. Done when it prints 5 numbers, sum 66, largest 23. This is the program no #define SIZE could write: how many numbers there are is not known until it runs.

  5. notes.txt — three short answers.

    1. In class, make_range could not hand back a when a was declared int a[SIZE], and gcc -Wall said so. Whose memory was that array, and what is different about the memory malloc hands out?
    2. join("point", "er") asks malloc for 8 bytes, not 7. What is the eighth byte for, and what does C promise will happen if you ask for 7?
    3. After free(t), the variable t still holds the address it held before. What does the reading say to do with t next, and why?
  6. 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. Write char * longest_word(char * s), which hands back a new string holding the longest word in s, a word being a run of characters that are not spaces; when s has 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. Your main:

    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.

  7. 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. Write int * 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’s divide handed back two answers. Your main:

    int main() {
        int n;
        int * a = read_all(&n);
        printf("%d numbers, largest %d\n", n, largest(a, n));
        free(a);
    }

    with largest written as in step 4. The design, in order:

    • read_all keeps two numbers: capacity, how many values the array has room for, and count, how many are in it so far. The array starts with room for START values, a #define of 4.
    • The reading loop is while (scanf("%d", &x) == 1): scanf returns how many values it read, and something else once the input has run out.
    • Before storing a value, check whether the array is full: count equal to capacity. When it is, make a new array with twice the room, copy the count values over, free the old array, and point a at the new one; capacity doubles. Write the copy as its own function, int * copy_into(int * a, int n, int size): a new array of size elements holding the n values of a.
    • Store the value at *(a + count), and count goes up by one.
    • At the end, *count gets 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 count 9 and capacity 16, after three calls to malloc and two to free; the third free is main’s.

    Make a file of a hundred numbers with seq 1 100 > many.txt, then run ./readall < many.txt. Done when it prints 100 numbers, largest 100, and ./readall < numbers.txt from step 4 prints 6 numbers, largest 23 — six, because to this program the 5 on the first line is one more number.