简体   繁体   中英

Can a variable become null by left or right shift operation in c / c++?

i have a code snippet in which i assign a value to a variable , var . In between the variable is acted upon left/right shift operations. Can such a variable ever become null.

Note that null is mostly defined as

#define null ((void*)0)

If it can't be null, then a check like:

if (var == null)

will give rise to dead code which will never be executed.

Built-in shift operators are only applicable to values of integral or unscoped enum types. Built-in comparison to your null is only legal for values of pointer types. This means that results of built-in shift can never be compared to your null . (The only potential loophole here is the possibility to use shifts to form a null-pointer constant - an integral zero. But this is only possible in pre-C++11 version of the language. Not anymore.) The comparison is invalid.

In other words, there's no valid context in which this question can even arise. Your if will not produce "dead code" or "alive code". It simply won't compile at all.

But if you are talking about overloaded operators, then everything is possible. However, in that case your question is too broad to make sense.

In C, you can compare interger values to pointers, so that code would produce a warning but probably work as expected. (Definitely not recommended, though)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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