/* Credits to author Filip Strömbäck */ #include #include #include #include #include /* Recommended compile command: * * make debug2 * * Run your code: * * ./debug2 * * Debug with GDB: * * gdb -tui ./debug2 */ // Create an array of numbers. // Note: not very good random numbers, but good enough for this example int *create_numbers(int count) { srand(time(NULL)); int *result = malloc(sizeof(int) * count); for (int i = 0; i < count; i++) result[i] = rand() % 512; return result; } // Print a count of numbers from an array void print_numbers(int *numbers, int count) { for (int i = 0; i < count; i++) { int number = numbers[i]; printf("Number %d: %d\n", i, number); } } // Print numbers with a headline above void print_with_header(const char *header, int *numbers, int count) { printf("%s\n", header); printf("------------------\n"); print_numbers(numbers, count); free(numbers); } void free_numbers(int** numbers) { free(*numbers); *numbers = NULL; } int main(void) { int count = 12; int *numbers = create_numbers(count); print_with_header("First time:", numbers, count); printf("\n"); print_with_header("Second time:", numbers, count); free(numbers); return 0; }