使用按位或时读取无效

Invalid read when using bitwise or

一不小心,我写了

if (var < 0 | List == NULL) {
   ...
}

其中 var 是一个 int 并且列出一个 int*(整数数组)。

本来想写的

if (var < 0 || List == NULL) {
   ...
} 

我知道

The operators |, &, and ~ act on individual bits in parallel. They can be used only on integer types. a | b does an independent OR operation of each bit of a with the corresponding bit of b to generate that bit of the result.

引自.

我不明白的是 valgrind(带选项 --leak-check=full)在第一种情况下给我错误 Invalid read of size 8,而在第二种情况下没有错误。

我认为 varList 的数据访问在这两种情况下是完全相同的。

List == NULL 的结果是什么类型?

"I think the data access to var and List is the very same in both cases."

不对,区别是short-circuit evaluation。使用逻辑 OR,表达式 List == NULL 将不会在 var < 0 时被计算。

但是使用按位或,您 计算 List == NULL,即使 var < 0 也是如此。为什么会触发 "Invalid read of size 8" 很难说,因为您没有显示足够的代码。但是我怀疑 Listvar < 0.

时没有初始化