c - 在 C 中查找数据模型

标签 c architecture datamodel

假设安全吗:

#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

适用于 Unix、BSD、Windows、Linux 和 HP?或者我完全错了 :-) 或者遗漏了什么?

谢谢

最佳答案

如果你真的没有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

预处理器使用与 uintmax_t 相同的宽度进行计算.因此,您至少会了解架构的那一部分。

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

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);

在合理的假设下(二进制补码,无填充位),您可以推断出带符号类型的值,并且您将能够执行正确的 typedef对于 uintXX_t类型。

关于c - 在 C 中查找数据模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11127293/

相关文章:

c++ - 您如何使用非 OO 语言进行继承?

c++ - 通过指向结构的指针访问结构中的值

c# - 以编程方式将表添加到 Microsoft SQL Server Compact 3.5 数据库

ios - 自定义获取核心数据(快速)

php - Laravel 中的数据库/模型字段名称约定?

c - C语言变量是如何存储的(在堆栈、列表、树中?)它们存储在哪里?(在ram/hdd中)?

c++ - 为什么我不能使用 fopen?

caching - 面试分配-设计像S3这样的系统

actionscript-3 - 游戏开发中的基于组件的架构

c++ - 为什么相同的 gcc 编译选项在不同的计算机体系结构上会有不同的表现?