A little thought reveals that the sum function should not allocate any memory. Here is a version that works, without a memory leak.
// sum(A,n) returns A[0] + A[1] + ... + A[n-1]. int sum(const int* A, int n) { int s = 0; int* p; for(p = A; p < A + n; p++) { s = s + *p; } return s; }