Search code examples
c++size-tint64

How does size_t and the __w64, time_t and __int64 work?


I am not a veteran in C or C++. I don't know the howtime_t is defined and designed. Several posts like:

But these posts only state what is time_t or size_t. It didn't state clearly how and where is time_t or size_t defined and declared.

I am using WIN8 VS2012 Express C++. I searched the library and found that size_t is defined in crtdefs.h instead of cstddef.

  • Why need types like size_t, time_t? We can let sizeof just return unsigned int.
  • Why C++ library does not define size_t?
  • Can I say size_t is NOT a C/C++ built-in type?
  • I never need to include the corresponding header file in my program to use size_t or time_t(maybe they are defined in std namespace).

I searched again in the library, and found size_t is defined as typedef _W64 unsigned int size_t, and _W64 is defined as __w64. Up here, I cannot search more on where is __w64 is defined or declared.

  • What is __w64 type? It is not stated in the C++11/C99 standard.

Similarly when I searched the time_t, I found __int6. But does not know where __int64 comes from.


Solution

  • The __w64 is a Microsoft specific extension that "enables warnings when compiling with /Wp64" - it is used to identify code that is "dangerous" if you are trying to compile it for 64-bit - for example assigning a 64-bit value to a 32-bit one. e.g.:

    size_t s;
    ...
    unsigned int i;
    
    ...
    i = s;    // 32 bit can't take all 64 bit values - possible problem. 
    

    The definition of size_t and time_t are typedef ... size_t; and typedef ... time_t; - they are not "fixed" in the compiler. The standard just says they have to be there (and be suitable for the system the compiler targets). I think you'll find that cstddef includes crtdefs.h in some place or another.

    The __int64 is a Microsoft compiler type. This is defined by the compiler itself. It can then be used to define other "sized types", such as time_t or size_t (with suitable unsigned as needed). Other compilers may not have this type, but there will be SOME way to define a 8, 16, 32 and 64-bit integer. The actual origin of __int64 is probably before the origin of long long, which for a long long time (pun intended) wasn't supported by Microsoft. I believe it is now.