I'm trying to create a two dimensional array but when i use free at the end of my program i always get "Segmentation fault (core dumped)" error. The sleep function is only used because i wanted to see if it crashes after creating the array or later, and the program crashes as soon as i use free(array)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void check(int number)
{
if(number < 0)
{
fprintf(stderr, "You cannot use a value below zero!\n");
}
}
int create_array(int **array, int size)
{
array = (int**)malloc(size * sizeof(int*));
for(int i = 0; i < size; i++)
{
array[i] = (int*)malloc(size * sizeof(int));
}
printf("Successfully created the array!\n");
printf("The size of the array is %d * %d = %d", size, size, sizeof(array) / sizeof(int));
return EXIT_SUCCESS;
}
int main(void)
{
int N;
printf("Please enter a value for N: ");
scanf("%d", & N);
check(N);
int R;
printf("Please enter a value for R: ");
scanf("%d", & R);
check(R);
int **array;
create_array(array, N);
sleep(1);
free(array);
return EXIT_SUCCESS;
}
malloc()is not the same value as that you pass tofree().