-1

My following code gives segmentation fault: 11 only when I add the clock() function to calculate the time elapsed (When I comment clock(), I get results with no issues!!! ):

typedef struct heap_strct *Sort;
struct heap_strct {
    int count;
    int size;
    int *queue;
};

int main() {
    time_t start = clock();
    Sort h;   // Sort is a structure
    initi(h);
    parse(h);
    time_t end = clock();
    double time_elapsed = (double)(end - start) / CLOCKS_PER_SEC;

    printf("Time = %f", time_elapsed);
}

I am using #include <time.h> but I don't know why such a fault appears! Kindly, can someone tell me why?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Michael
  • 179
  • 11
  • How do you know for sure? – Sourav Ghosh Oct 30 '16 at 18:16
  • 3
    You mind creating a [___MCVE___](http://stackoverflow.com/help/mcve)? – Sourav Ghosh Oct 30 '16 at 18:16
  • Because when I comment `clock()`, I get results – Michael Oct 30 '16 at 18:17
  • that may be very well outcome of UB. – Sourav Ghosh Oct 30 '16 at 18:17
  • 2
    [No, that's not why your program crashes](https://ideone.com/PGc0Zh) – StoryTeller - Unslander Monica Oct 30 '16 at 18:18
  • 1
    Have you tried with `clock_t` as the function declaration of `clock()` specifies? – StoryTeller - Unslander Monica Oct 30 '16 at 18:21
  • 1
    Post the definition of `Sort`, `initi` and `parse` – chqrlie Oct 30 '16 at 18:23
  • You are using the wrong type for `clock()` return value, it should be `clock_t` and not `time_t`. Apart from that your use of `clock()` is good. – Weather Vane Oct 30 '16 at 18:29
  • 2
    Please read about how to create an MCVE ([MCVE]) — and then provide us with one. What you're seeing is probably a consequence of undefined behaviour somewhere in either `initi()` or `parse()`. – Jonathan Leffler Oct 30 '16 at 18:31
  • 2
    You pass an uninitialized pointer to `initi()`, which has no way to set that pointer. You then pass that (still) uninitialized pointer to `parse()`. It is no wonder that your code crashes. You probably need to revise `initi()`, either so that its interface is `void initi(Sort *h)` (called with `initi(&h);`) or so that its interface is `Sort *initi(void)` (called with `Sort h = initi();`). See also [Is it a good idea to typedef pointers?](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers) — the short answer is "No". – Jonathan Leffler Oct 30 '16 at 18:32
  • 1
    "when I add X to my program, it starts crashing, therefore X is responsible for crashes" --- this looks like sound logic, but *it's not*. – n. m. could be an AI Oct 30 '16 at 18:34
  • Commenting out the part that you think is at fault, can be like trying to get bumps out of the carpet - you can displace the *effect* of the bug to somewhere else, perhaps where the harm is less immediate. – Weather Vane Oct 30 '16 at 18:35

2 Answers2

1

You pass an uninitialized pointer to function initi(). If this function modifies the structure, you invoke undefined behavior.

It is a very bad habit to hide pointers behind typedefs. The comment is completely misleading: Sort is not a structure!

Define a structure directly and pass its address:

#include <time.h>

struct heap_strct {
    int count;
    int size;
    int *queue;
};

int main(void) {
    clock_t start = clock();
    struct heap_strct h;   // h is a structure for real now!
    initi(&h);
    parse(&h);
    time_t end = clock();
    double time_elapsed = (double)(end - start) / CLOCKS_PER_SEC;

    printf("Time = %f\n", time_elapsed);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

clock() is a proven library function and will not cause a segfault.

You pass a pointer h to initi but there is no storage for h allocated. Then you pass the same pointer to parse. But there still is no storage for the structure!

When you comment clock() out, the call is not made so the stack is not changed. On the stack is also h, an uninitialized local variable.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41