Q: segmentation fault calloc in C I am making a C program that takes the average time of a calloc, malloc, and alloca process. I got everything to compile, but when I run it I get a segmentation fault. The first thing it runs is calloc so I am going to assume the problem starts there.
Here is my calloc function, the malloc and alloca are basically the same so I figure there is no reason to post them yet.
double calloctest(int objectsize, int numberobjects, int numberoftests)
{
    double average = 0;
    for (int i = 0; i < numberoftests; i++)
    {
            clock_t begin = clock();
            int *objectsize = calloc(numberobjects, sizeof(char) * *objectsize);
            clock_t end = clock();
            double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
            average = average + time_spent;
            printf("%f", time_spent);
            free(objectsize);
    }
    double totalAverage;
    totalAverage = average / numberoftests;
    return totalAverage;
}


A: You have a local variable objectsize that shadows the function argument with the same name and you dereference it before storing the return value of calloc():
int *objectsize = calloc(numberobjects, sizeof(char) * *objectsize);

You probably meant to write:
int *object = calloc(numberobjects, objectsize);
...
free(object);

