The size of time_t and its maximum value

#include <stdio.h> #include <time.h> int main() { printf("Size of time_t is %lu bytes.\n", sizeof(time_t)); time_t biggest = 0x7fffffffffffffff; // line 1 printf("time_t wrap around will be a second after %s.\n", asctime(gmtime(&biggest)) ); return 0; } 

In time.h, is that the definition of time_t?

 typedef __darwin_time_t time_t 

How to interpret this? Since I have no idea, I used the sizeof function to find time_t = 8 bytes.

Why does line 1 give an error? I get this error

 Segmentation fault: 11 
+7
source share
4 answers

Your call to gmtime() probably returns NULL (it works on my Mac OS X system). When you pass this value to asctime() , you will get an exception (since it expects a non- NULL pointer).

Not all time_t values ​​can be represented in struct tm , so gmtime() returns NULL in this case.

+8
source

I looked at the Linux header files and found the following things:

 /usr/include/time.h: typedef __time_t time_t; /usr/include/bits/types.h: __STD_TYPE __TIME_T_TYPE __time_t /usr/include/bits/typesizes.h #define __TIME_T_TYPE __SLONGWORD_TYPE /usr/include/bits/types.h: #define __SLONGWORD_TYPE long int 

So the size of time_t is long int . On a 64-bit machine, you will probably get 8 bytes. Then I found that on my FreeBSD, which is 32 bits, it looked like this:

 /usr/include/machine/_types.h: typedef __int32_t __time_t; 

So, the size is 32 bits.

+3
source

The main type of time_t can be anything. It depends a lot on your system. Please refer to a similar question: What is the primitive data type time_t? .

There is a link in the stream in the description of sys / types.h , which indicates that

time_t and clock_t must be integer or real floating types.

Another link to a good answer: stack overflow

+2
source

Although the size of time_t is 8, on a 64-bit platform there are restrictions defined in the implementation, hardcoded at run time, which cannot be exceeded. When these limits are exceeded, gmtime will return NULL with errno set to indicate the nature of the error. Your code cannot check for NULL returns from gmtime, so asctime does not work when it tries to dereference a null pointer. This is a danger that you risk when using idiomatic C and do not check return values ​​from functions that may fail.

Microsoft C gmtime on Windows 10 returns NULL with errno set to EINVAL when passing 32535291600. Although you might think that the theoretical maximum limit should be LLONG_MAX. Microsoft explicitly sets a limit on _MAX__TIME64_T + _MAX_LOCAL_TIME.

From ctime.h in Microsoft Visual Studio 2010 CRT sources:

 #define _MAX__TIME64_T 0x793406fffi64 /* number of seconds from 00:00:00, 01/01/1970 UTC to 23:59:59. 12/31/3000 UTC */ 

The Apple gmtime function returns NULL and errno set to EOVERFLOW when it passed 67768036191676800. The Apple implementation shows undefined behavior for all times when asctime () returns a string with more than 4 digits per year, because for standard C the function returns no more 26 characters.

+2
source

All Articles