C/C++ Arena

Returning heap memory from a function

repeat builds a string on the heap and returns its address. Its own frame disappears when it returns, but the heap block stays, and now main's s points at it.

That transfer is ownership: main now owns the block and must free it. A local array in repeat would have died with its frame.

#include <stdio.h>
#include <stdlib.h>

char *repeat(char c, int n) {
    char *s = malloc(n + 1);
    if (!s) return NULL;
    for (int i = 0; i < n; i++) {
        s[i] = c;
    }
    s[n] = '\0';
    return s;
}

int main(void) {
    char *s = repeat('z', 3);
    if (!s) return 1;
    printf("%s\n", s);
    free(s);
    return 0;
}

Output:

zzz

From the lesson: Dynamic memory