Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

time(NULL); vs time(&something);

Tags:

c

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.

like image 377
LukeN Avatar asked May 28 '10 11:05

LukeN


People also ask

What does time null mean?

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>

What does time null mean in C++?

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.


1 Answers

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.

like image 62
Cobusve Avatar answered Oct 05 '22 04:10

Cobusve