利用C语言"位域"的特殊性来对数组进行边界检查

1.序

数组越界时常有发生,如果我们能够让编译器提醒我们犯错那是最好。

2.例子

#include <stdio.h>
#include <stdlib.h>

#define ARRAY_CARDINALITY(Array) (sizeof(Array) / sizeof(*(Array)))

#undef YIN_VERIFY
#define YIN_VERIFY(name,cond)   static char name ## dummy [sizeof(struct { unsigned char dummy:((cond)?1:-1);})] __attribute__ ((unused))

int yin_util_str2enum(const char *const*types,
                              unsigned int ntypes,
                              const char *type);

const char *yin_util_enum2str(const char *const*types,
                                    unsigned int ntypes,
                                    int type);


#define YIN_ENUM_IMPL(name, lastVal, ...)                               \
    static const char *const name ## TypeList[] = { __VA_ARGS__ };      \
    YIN_VERIFY(name,(ARRAY_CARDINALITY(name ## TypeList) == lastVal));  \
    const char *name ## TypeToString(int type) {                        \
        return yin_util_enum2str(name ## TypeList,                      \
                               ARRAY_CARDINALITY(name ## TypeList),     \
                               type);                                   \
    }                                                                   \
    int name ## TypeFromString(const char *type) {                      \
        return yin_util_str2enum(name ## TypeList,                      \
                                 ARRAY_CARDINALITY(name ## TypeList),   \
                                 type);                                 \
    }

#define YIN_ENUM_DECL(name)                             \
    const char *name ## TypeToString(int type);         \
    int name ## TypeFromString(const char*type);


/* Exported functions ------------------------------------------------------- */
int yin_util_str2enum(const char *const*types,
                              unsigned int ntypes,
                              const char *type)
{
    size_t i;
    if (!type)
        return -1;

    for (i = 0; i < ntypes; i++){
        if (0 == strcmp(types[i], type))
            return i;
    }

    return -1;
}

const char * yin_util_enum2str(const char *const*types,
                                  unsigned int ntypes,
                                  int type)
{
  if (type < 0 || type >= ntypes)
      return NULL;

  return types[type];
}

//
enum {
  EUNKOW,
  ENAME,
  EAGE,
  EBUTT
};

YIN_ENUM_IMPL(student,EBUTT,"uknow","name")


int
main(int argc, char *argv[])
{
    return 0;
}

执行报错:

enum2str.c:7:72: error: negative width in bit-field 'dummy'
 #define YIN_VERIFY(name,cond) static char name ## dummy [sizeof(struct { unsigned char dummy:((cond)?1:-1);})] __attribute__ ((unused))
                                                                        ^
enum2str.c:20:2: note: in expansion of macro 'YIN_VERIFY'
  YIN_VERIFY(name,(ARRAY_CARDINALITY(name ## TypeList) == lastVal));  \
  ^
enum2str.c:72:1: note: in expansion of macro 'YIN_ENUM_IMPL'
 YIN_ENUM_IMPL(student,EBUTT,"uknow","name")
 ^

将 YIN_ENUM_IMPL(student,EBUTT,”uknow”,”name”) 改为 YIN_ENUM_IMPL(student,EBUTT,”uknow”,”name”,”age”) 错误解决。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值