This
#include <stdlib.h> // test.c
int main()
{
double *arr = (double*) calloc(1000, sizeof(arr));
for (int i = 0; i < 1000; i++)
arr[i] = 1;
free(arr);
return 0;
}
compiled as
gcc test.c -std=c99 -o test
produces the output Aborted (core dumped)
when run on Cygwin as ./test.exe
.
The program exits fine when I declare int *arr
(even while keeping the (double *)
cast), or when I update arr[0]
instead of arr[i]
. What gives?
Change
double *arr = (double*) calloc(1000, sizeof(arr));
to
double *arr = (double*) calloc(1000, sizeof(double));
UPDATE:
sizeof(double)
and sizeof(pointer)
depends on architecture. In your case they are different.