// Cuenta las llamadas a realloc al agregar 20000 enteros, con dos reglas.
#include <stdio.h>
#include <stdlib.h>

#define TOTAL 20000

// factor 0: crece de uno en uno. factor 2: duplica la capacidad.
// Devuelve cuántas veces llamó a realloc, o -1 si no hubo memoria.
long fill(int factor) {
    int *items = NULL;
    size_t capacity = 0;
    long calls = 0;

    for (size_t count = 0; count < TOTAL; count++) {
        if (count == capacity) {
            size_t wanted = factor == 0 || capacity == 0 ? capacity + 1 : capacity * factor;
            int *bigger = realloc(items, wanted * sizeof *items);
            if (bigger == NULL) {
                free(items);
                return -1;
            }
            items = bigger;
            capacity = wanted;
            calls++;
        }
        items[count] = (int) count;
    }

    free(items);
    return calls;
}

int main(void) {
    printf("de uno en uno: %ld llamadas\n", fill(0));
    printf("duplicando:    %ld llamadas\n", fill(2));
    return 0;
}
