pointers-memory · intermediate · ~15 min

Allocate, use, free

Complete the alloc/use/free lifecycle in one function.

Challenge

Implement long sum_then_free(int n) that allocates an n-int array, fills it with 1..n, computes the sum, frees the array, and returns the sum. No leaks.

Starter code

#include <stdlib.h>

long sum_then_free(int n) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.