简体   繁体   English

如何将size_t和pid_t与int比较

[英]How to compare size_t and pid_t with int

What is the right way to use logical operators with size_t and pid_t types? size_tpid_t类型中使用逻辑运算符的正确方法是什么? I mean: is legal to write something like the following? 我的意思是:写以下内容合法吗?
eg: 例如:

size_t sz;  
/* some kind of assignment */  
if(sz > 0){  
  /* do something */  
}  

eg: 例如:

void f(pid_t pid,...){  
  if(pid > 0){  
    /* do something */  
  }  
  /* ... */  
}

..or I have to do some cast? ..还是我必须做一些演员?

EDIT 编辑
ok for answers; 好的答案;
now, considered what has been told, can someone give me a reason for coding that way: 现在,考虑到所讲的内容,有人可以给我理由以这种方式进行编码:

void *my_malloc(size_t size){
  if(size <= 0){
    return NULL;
  }
  /* something else... */
}  

My teacher wrote that code. 我的老师写了那个代码。
Does it make sense? 是否有意义?
I don't know how many bits are reserved for size_t type(it is implementation-dependent) 我不知道为size_t类型保留多少位(取决于实现)
but surely it is an unsigned(your answer), so why the above expression? 但是肯定是一个未签名的(您的答案),那么为什么要使用上面的表达式?

Yes, it's legal. 是的,这是合法的。 Both arguments to > will be promoted to a matching type before the comparison is made. 在进行比较之前, >两个参数都将被提升为匹配类型。

Be aware that for an unsigned type such as size_t , > 0 means the same as != 0 . 请注意,对于无符号类型(例如size_t> 0表示与!= 0相同。

size_t and pid_t really are integer values of different flavours -- so yes. size_tpid_t实际上是不同pid_t的整数值-是的。

From what I understand, the whole point behind them, indeed, is that the flavour of size_t and pid_t may vary between implementations, OSes and architectures (say, 32 bit v 64 bit) and whatnot. 据我了解,它们背后的全部意思是, size_tpid_t的风格可能在实现,操作系统和体系结构(例如32位v 64位)之间有所不同。

In C, size_t is an unsigned type and its size is the size an int type takes on the underlying architecture. 在C中,size_t是无符号类型,其大小是int类型对基础体系结构的接受大小。

Because C is weakly typed you could however assign signed integers to a size_t type. 由于C是弱类型的,因此您可以将带符号的整数分配给size_t类型。 The responsibility of using the types properly partly rests on the programmer. 正确使用类型的责任部分在于程序员。

In your case since you are comparing the size_t type to zero it is fine. 在您的情况下,由于您将size_t类型比较为零,所以很好。 Try comparing it to a negative number. 尝试将其与负数进行比较。 You would be surprised. 您会感到惊讶。

There's no problem in comparing a size_t value with an int value as long as you remember that the int value will be implicitly converted to unsigned type (since size_t is unsigned). 只要您记住将int值隐式转换为无符号类型(因为size_t是无符号的),就可以将size_t值与int值进行比较没有问题。

Because of the above, some compilers will issue a warning when you mix signed and unsigned types in comparisons. 由于上述原因,当您在比较中混合带符号和无符号类型时,某些编译器将发出警告。 In that case, in order to suppress the warning you'd have to explicitly convert the signed value to an appropriate unsigned type. 在这种情况下,为了消除警告,您必须将带符号的值显式转换为适当的无符号类型。 However, these compilers don't normally issue any warnings when the signed value is a non-negative compile-time constant, meaning that in your example there's no need to cast an explicit 0 to unsigned type (or use 0U ). 但是,当有符号值是非负编译时常量时,这些编译器通常不会发出任何警告,这意味着在您的示例中,无需将显式0 0U为无符号类型(或使用0U )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM