Question

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.

Était-ce utile?

La 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.

Autres conseils

C/C++ has 20+ years legacy and it is implemented now maybe on 100+ different architectures.

In fact, some of them are really different from x86. On some of them integer registers are 16 bit, while the address space and address registers are 32 bit. I have experience writing in these environments.

Yes, in this case int/unsigned int is 16 bits, and this makes sense, while pointer and size_t are 32 bits. On x86 this is not so, but well...

This is the answer.

size_t is NOT a C/C++ built-in type?

The true answer is yes and no. Theoretically, you can write your own header files, like crtdefs.h. And write there whatever you want. The compiler most likely will work. But this is a lot of work. Nobody is doing that. Once system header files are part of compiler, then well, size_t is built in....

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top