Real simple question. According to my man-page, these two will do the same:
time_t t;
time(&t);
printf("Time: %ld", t);
...
printf("Time: %ld", time(NULL));
So, what exactly is the benefit of passing a pointer to time? In case time()
would fail due to no clock being available, neither of the two variants will have any benefit in my opinion.
time(NULL) returns the number of seconds elapsed since 00:00:00 hours, GMT (Greenwich Mean Time), January 1, 1970. Example: #include<iostream.h> #include<time.h>
the time(NULL) Function in C++ The time() function with a parameter NULL, time(NULL) , returns the current calendar time in seconds since 1 January 1970. Null is a built-in constant whose value is 0 and a pointer similar to 0 unless the CPU supports a special bit pattern for a Null pointer.
The benefit would be that you do not have to copy the data into another structure after calling 'time'.
If you are e.g. preparing a buffer of data to send to another application/server you would have to copy the data, which is additional overhead. By passing a pointer into your data structure you could put it right in the correct place in one shot.
Of course if your only use for the data is to convert it to another format such as a text printf, then it is more efficient to call it with NULL and save the additional storage required by your first example.
Lastly since the time function uses a single location for storage of it's internal time structure the first method would be more thread-safe, although I cannot remember of the top of my head if 'time' is in fact thread safe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With