C 中标准类型的 typedef 的推荐方法是什么?
例如,在我的项目开始时,我创建了 typedef 以使用尽可能小的类型来实现其目的。主要目的是在使用的数值范围对于类型来说太大的情况下允许轻松修改类型,而无需对整个项目进行全局搜索+替换。但六个月后我不满意,认为必须有更好的方法。
这些是我定义的类型:
/* type to be used for storing map data within
n*n array */
typedef unsigned char map_t;
/* type to be used for storing coordinates in map.
min 0,max 32 (MAP_W,MAP_H defaults). note: no
longer requires signed. */
typedef unsigned char xy_t;
/* type to be used for counters, ie moves_remaining
has max 2000 */
typedef signed int ctr_t;
/* type to be used for storing the level number,
requires signed. */
typedef signed short lvl_t;
/* type to be used for storing contact types,
see actions.h CONTACT */
typedef signed short ct_t;
/* small unsigned type, miscellaneous */
typedef unsigned char su_t;
/* small signed type, miscellaneous */
typedef signed char ss_t;
struct xy
{
xy_t x;
xy_t y;
};
struct scrxy
{
int x;
int y;
};
struct xy_offset
{
ss_t x;
ss_t y;
};
一些代码,而不是使用有符号类型并检查n < 0 && n > max_val
使用无符号类型,并且只检查n > max_val
where max_val
is less theT_MAX
尽管我已经发现这种行为实际上是未定义的。
我是否应该删除所有这些typedef
s 并在stdint.h
整个代码中使用定义的标准类型(现在大小已经相当稳定了)?