Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
It depends. This boils down to whether or not the expression cast to void contains any side effects, such as accessing a volatile-qualified object or modifying any object. C17 6.3.2.2: If an ex...
Answer
#3: Post edited
- It depends. This boils down to whether or not the expression cast to `void` contains any _side effects_, such as accessing a `volatile`-qualified object or modifying any object.
- C17 6.3.2.2:
- > If an expression of any other type is evaluated as a void
- expression, its value or designator is discarded. (A `void` expression is evaluated for its side effects.)
- In your example the cast operand does not contain any side effects so it is simply discarded, meaning that it is safe.
This would not be fine however:- ```c
- {
- volatile int x;
- (void)x; // undefined behavior, lvalue access of local uninitialized variable
- }
- ```
- While this is ok:
- ```c
- {
- int x;
- (void)(x=1); // well-defined, x is set to 1
- (void)function(x); // well-defined, function is called and the result discarded
- }
- ```
Regarding all the details of when it is undefined behavior to use a uninitialized variable with indeterminate value, check out this answer: [(Why) is using an uninitialized variable undefined behavior?](https://stackoverflow.com/a/40674888/584518) The explanation is not as trivial as "this is always ok" or "this is always UB". The type of the variable matters a lot - plain integers are for example very unlikely to contain trap representations, but floating point variables or pointers (particularly) may do.- So this is not ok either:
- ```c
- {
- int x,y;
- (void)(x=y); // undefined behavior, lvalue access of local uninitialized variable y
}
- It depends. This boils down to whether or not the expression cast to `void` contains any _side effects_, such as accessing a `volatile`-qualified object or modifying any object.
- C17 6.3.2.2:
- > If an expression of any other type is evaluated as a void
- expression, its value or designator is discarded. (A `void` expression is evaluated for its side effects.)
- In your example the cast operand does not contain any side effects so it is simply discarded, meaning that it is safe.
- This would not be fine, however:
- ```c
- {
- volatile int x;
- (void)x; // undefined behavior, lvalue access of local uninitialized variable
- }
- ```
- While this is ok:
- ```c
- {
- int x;
- (void)(x=1); // well-defined, x is set to 1
- (void)function(x); // well-defined, function is called and the result discarded
- }
- ```
- Regarding all the details of when it is undefined behavior to use an uninitialized variable with an indeterminate value, check out this answer: [(Why) is using an uninitialized variable undefined behavior?](https://stackoverflow.com/a/40674888/584518) The explanation is not as trivial as "this is always ok" or "this is always UB". The type of the variable matters a lot - plain integers are for example very unlikely to contain trap representations, but floating-point variables or pointers (particularly) may do.
- So this is not ok either:
- ```c
- {
- int x,y;
- (void)(x=y); // undefined behavior, lvalue access of local uninitialized variable y
- }
- ```
#2: Post edited
- It depends. This boils down to whether or not the expression cast to `void` contains any _side effects_, such as accessing a `volatile`-qualified object or modifying any object.
- C17 6.3.2.2:
- > If an expression of any other type is evaluated as a void
- expression, its value or designator is discarded. (A `void` expression is evaluated for its side effects.)
- In your example the cast operand does not contain any side effects so it is simply discarded, meaning that it is safe.
- This would not be fine however:
- ```c
- {
- volatile int x;
- (void)x; // undefined behavior, lvalue access of local uninitialized variable
- }
- ```
- While this is ok:
- ```c
- {
- int x;
- (void)(x=1); // well-defined, x is set to 1
- (void)function(x); // well-defined, function is called and the result discarded
- }
- ```
Regarding all the details of when it is undefined behavior to use a uninitialized variable with indeterminate value, check out this answer: [(Why) is using an uninitialized variable undefined behavior?](https://stackoverflow.com/a/40674888/584518) The explanation is not as trivial as "this is always ok" or "this is always UB". The type of the variable matters a lot - plain integers are for example very unlikely to contain trap representations, but floating point variables or pointers (particularly) may do.
- It depends. This boils down to whether or not the expression cast to `void` contains any _side effects_, such as accessing a `volatile`-qualified object or modifying any object.
- C17 6.3.2.2:
- > If an expression of any other type is evaluated as a void
- expression, its value or designator is discarded. (A `void` expression is evaluated for its side effects.)
- In your example the cast operand does not contain any side effects so it is simply discarded, meaning that it is safe.
- This would not be fine however:
- ```c
- {
- volatile int x;
- (void)x; // undefined behavior, lvalue access of local uninitialized variable
- }
- ```
- While this is ok:
- ```c
- {
- int x;
- (void)(x=1); // well-defined, x is set to 1
- (void)function(x); // well-defined, function is called and the result discarded
- }
- ```
- Regarding all the details of when it is undefined behavior to use a uninitialized variable with indeterminate value, check out this answer: [(Why) is using an uninitialized variable undefined behavior?](https://stackoverflow.com/a/40674888/584518) The explanation is not as trivial as "this is always ok" or "this is always UB". The type of the variable matters a lot - plain integers are for example very unlikely to contain trap representations, but floating point variables or pointers (particularly) may do.
- So this is not ok either:
- ```c
- {
- int x,y;
- (void)(x=y); // undefined behavior, lvalue access of local uninitialized variable y
- }
#1: Initial revision
It depends. This boils down to whether or not the expression cast to `void` contains any _side effects_, such as accessing a `volatile`-qualified object or modifying any object. C17 6.3.2.2: > If an expression of any other type is evaluated as a void expression, its value or designator is discarded. (A `void` expression is evaluated for its side effects.) In your example the cast operand does not contain any side effects so it is simply discarded, meaning that it is safe. This would not be fine however: ```c { volatile int x; (void)x; // undefined behavior, lvalue access of local uninitialized variable } ``` While this is ok: ```c { int x; (void)(x=1); // well-defined, x is set to 1 (void)function(x); // well-defined, function is called and the result discarded } ``` Regarding all the details of when it is undefined behavior to use a uninitialized variable with indeterminate value, check out this answer: [(Why) is using an uninitialized variable undefined behavior?](https://stackoverflow.com/a/40674888/584518) The explanation is not as trivial as "this is always ok" or "this is always UB". The type of the variable matters a lot - plain integers are for example very unlikely to contain trap representations, but floating point variables or pointers (particularly) may do.