在C中查找数据模型

时间:2021-02-11 17:00:15

Is it safe to assume:

假设是安全的:

#if defined(_M_X64) || defined(_M_AMD64) || (defined(_M_IA64) && !defined(__itanuim__))
/* LLP64 Data Model */
#elif defined(__amd64__) || defined(__ia64__) || defined(__ia64) || defined(__itanuim__)
/* LP64 Data Model */
#else
/* 32-bits Data Model(s) */
#endif

for Unix, BSD, Windows, Linux and HP? or I'm totally wrong :-) or missing something?

对于Unix,BSD,Windows,Linux和HP?或者我完全错了:-)或遗失了什么?

Thanks

1 个解决方案

#1


0  

If you really don't have UINT_MAX and Co available through include files you can at least determine UINTMAX_MAX:

如果您确实没有通过包含文件提供UINT_MAX和Co,则至少可以确定UINTMAX_MAX:

typedef unsigned long long uintmax_t;
typedef signed long long intmax_t;

// start testing with the smaller values
#if -1U == 4294967295U
# define UINTMAX_MAX 4294967295
#elif -1U == 18446744073709551615U
...
#endif

The preprocessor computes with the same width as uintmax_t. So you'd know at least that part of the architecture.

预处理器的计算宽度与uintmax_t相同。所以你至少知道这部分架构。

For other things I would write a test code that creates a small include file:

对于其他事情,我会编写一个测试代码来创建一个小的包含文件:

printf("#define CHAR_IS_SIGNED %d\n", ((char)-1 < 0));
printf("#define UCHAR_MAX %hhu\n", (unsigned char)-1);
printf("#define USHRT_MAX %hu\n", (unsigned short)-1);
printf("#define UINT_MAX %u\n", (unsigned)-1);
printf("#define ULONG_MAX %lu\n", (unsigned long)-1);
printf("#define ULLONG_MAX %llu\n", (unsigned long long)-1);

Under reasonable assumptions (two's complement, no padding bits) you may then deduce the values for the signed types and also you'd be able to do the correct typedef for the uintXX_t types.

在合理的假设下(二进制补码,没有填充位),您可以推导出有符号类型的值,并且您还可以为uintXX_t类型执行正确的typedef。

#1


0  

If you really don't have UINT_MAX and Co available through include files you can at least determine UINTMAX_MAX:

如果您确实没有通过包含文件提供UINT_MAX和Co,则至少可以确定UINTMAX_MAX:

typedef unsigned long long uintmax_t;
typedef signed long long intmax_t;

// start testing with the smaller values
#if -1U == 4294967295U
# define UINTMAX_MAX 4294967295
#elif -1U == 18446744073709551615U
...
#endif

The preprocessor computes with the same width as uintmax_t. So you'd know at least that part of the architecture.

预处理器的计算宽度与uintmax_t相同。所以你至少知道这部分架构。

For other things I would write a test code that creates a small include file:

对于其他事情,我会编写一个测试代码来创建一个小的包含文件:

printf("#define CHAR_IS_SIGNED %d\n", ((char)-1 < 0));
printf("#define UCHAR_MAX %hhu\n", (unsigned char)-1);
printf("#define USHRT_MAX %hu\n", (unsigned short)-1);
printf("#define UINT_MAX %u\n", (unsigned)-1);
printf("#define ULONG_MAX %lu\n", (unsigned long)-1);
printf("#define ULLONG_MAX %llu\n", (unsigned long long)-1);

Under reasonable assumptions (two's complement, no padding bits) you may then deduce the values for the signed types and also you'd be able to do the correct typedef for the uintXX_t types.

在合理的假设下(二进制补码,没有填充位),您可以推导出有符号类型的值,并且您还可以为uintXX_t类型执行正确的typedef。