【发布时间】:2012-03-15 14:03:51
【问题描述】:
我需要编写一个在 bitarray 上运行的宏,如下所示:
array[0] = number of bits in bitarray (integer)
array[1..n] = bits
宏必须如下所示:
GetBit(pointer, index)
Macro *must* return 0,1 or call function similar to exit().
这是我应该编写的(工作)内联函数版本的宏:
static inline unsigned long GetBit(BitArray_t array, unsigned long index)
{
if ((index) >= array[0])
exit(EXIT_FAILURE);
else
return (GetBit_wo_boundary_checks(array,index));
}
这就是我所拥有的:
#define GetBit(array,index)\
(((index) < array[0] || exit(EXIT_FAILURE)) ?\
GetBit_wo_boundary_checks(array,index) : 0)
我的问题是,这个必须在尝试使用 GetBit_wo_boundary_checks(p,i) 访问未定义的内存之前进行索引边界检查 (i
我认为我可以通过将出口置于短路评估条件来解决此问题,但我得到:“无效使用 void 表达式”。
当索引高于数组[0]中定义的最大值时,有什么方法可以让这个表达式宏透明地退出()?
【问题讨论】:
标签: c c-preprocessor