logo

Memory leaks and how to prevent them

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> void random_array(int* arr, int n) { srand(0); int i; for (i = 0; i < n; i++) { arr[i] = rand() % 100; } } int main(int argc, char* argv[]) { int* arr = malloc(sizeof(int) * 15); int* arr_copy = arr; random_array(arr, 15); int i; for (i = 0; i < 15; i++) { printf("%d\n", *(arr_copy++)); } arr_copy = NULL; free(arr); return 0; }
Course thumb

The C programming language made simple