我不知道这一行发生了什么num && *num == maxNum。为什么var && *var可以用maxbound来判断?我真的需要有人来解释一下这个......
void func(...,size_t *_Nullable num,...){
size_t num_=0;
somethingHappen_get_num(&num_);
if(num){
*num = num_;
}
if (num && *num == maxNum) { //why???????????
Log_print("num is too big");
return Error;
}
}
Run Code Online (Sandbox Code Playgroud)
因为num是size_t *_Nullable numie 指针类型,所以你应该总是检查它是否为空 ieNULL或者有一些有效的数据,如果它有效,那么只有它可以安全地引用(*num)它。
这里
if (num && (*num == maxNum)) { } /* safe & correct, && means if 1st operand is valid then only check 2nd operand which is recommended here */
Run Code Online (Sandbox Code Playgroud)
条件检查是正确且安全的,因为首先它检查是否num不正确NULL,如果这是真的,则仅检查*num == maxNum其中是否有东西num以便您可以执行操作*num。
考虑下面的场景
if (num || (*num == maxNum)) { } /* bad practice, don't do it */
Run Code Online (Sandbox Code Playgroud)
num如果是,上述条件检查会产生问题NULL,由于逻辑或操作数,||它将检查第二个操作数,因为第一个操作数为假,并且在执行时会导致崩溃*num。