错误 "Stack around variable was corrupted"

Error "Stack around variable was corrupted"

错误 "Stack arround variable BITS was corrupted",MSVS 2013,C99。 为什么它不起作用? 如果我分配

NAME[(LENGTH) / CHAR_BIT + 1]

程序会正常运行。

#include <stdio.h>
#include <limits.h>

#define CREATE_BIT_FIELD(NAME, LENGTH) \
    unsigned char NAME[(LENGTH) / CHAR_BIT + ((LENGTH) % CHAR_BIT) ? 1 : 0]

#define SET_BIT(NAME, N) \
    NAME[(N) / CHAR_BIT] |= ((unsigned char)1 << ((N) % CHAR_BIT))

#define CLEAR_BIT(NAME, N) \
    NAME[(N) / CHAR_BIT] &= (UCHAR_MAX - ((unsigned char)1 << ((N) % CHAR_BIT)))

#define IS_SETTED(NAME, N) \
    (NAME[(N) / CHAR_BIT] & ((unsigned char)1 << ((N) % CHAR_BIT)))

int main(void)
{ 
    CREATE_BIT_FIELD(BITS, 16);

    for (int i = 0; i < 16; i++)
        SET_BIT(BITS, i);

    for (int i = 0; i < 16; i++)
        putchar(IS_SETTED(BITS, i) ? '1' : '0');

    getchar();
    return 0;
}

这个

unsigned char NAME[(LENGTH) / CHAR_BIT + ((LENGTH) % CHAR_BIT) ? 1 : 0]

总是被评估为 1 添加括号

unsigned char NAME[(LENGTH) / CHAR_BIT + (((LENGTH) % CHAR_BIT) ? 1 : 0)]
                                      /* ^ here                        ^ and here */

否则计算为

((LENGTH) / CHAR_BIT + (((LENGTH) % CHAR_BIT)) ? 1 : 0

这是因为运算符的优先级。