2

I'm trying to measure real, usr and sys times multiple times. I've made function

void get_clock(clock_t real_time, struct tms cpu_time){
    real_time = times(&cpu_time);
}

to use for gathering start and end times:

clock_t st_time;
clock_t en_time;
struct tms st_cpu;
struct tms en_cpu;

and it seems to work great the first time I use get_clock() for start and end, but every time difference I get is the same, which I believe is because the values are not getting updated.

Whazz
  • 367
  • 2
  • 12
  • `cpu_time` is only local to `get_clock`, so the caller of `get_clock` won't see the change. `get_clock` should get pointers. – Pablo Mar 22 '18 at 22:11
  • Possible duplicate of [Parameter Passing in C - Pointers, Addresses, Aliases](https://stackoverflow.com/questions/29088971/parameter-passing-in-c-pointers-addresses-aliases) – Yunnosch Mar 22 '18 at 22:12
  • 1
    also please provide a [mcve], we don't know how you call your functions. – Pablo Mar 22 '18 at 22:14
  • right, thanks @Pablo I should've payed more attention – Whazz Mar 22 '18 at 22:15
  • Quick quiz for @Whazz: `void f(int i) {i = 1;} int main() {int x = 5; f(x); printf("%d\n", x); return 0;}` // what will be the output? – user253751 Mar 22 '18 at 22:17
  • @immibis well, the f(x) gets just the copied value of int x, not actual address in memory where the 5 is, so in this case f() does nothing. That was serious overlook on my part and I should be and am ashamed, but RN time is of the essence for me. – Whazz Mar 22 '18 at 22:23
  • Then I think you have your answer for why `get_times` does nothing. – user253751 Mar 22 '18 at 22:32
  • 1
    Your `get_clock()` function should probably return a result of type `clock_t`. – Keith Thompson Mar 22 '18 at 22:36

0 Answers0