Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

77%
+5 −0
Q&A Cast uninitialized variable to (void)

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...

posted 1y ago by Lundin‭  ·  edited 1y ago by Ethan‭

Answer
#3: Post edited by user avatar Ethan‭ · 2022-09-15T06:47:23Z (over 1 year ago)
  • 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 by user avatar Lundin‭ · 2022-09-12T08:40:05Z (over 1 year ago)
  • 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 by user avatar Lundin‭ · 2022-09-12T08:33:16Z (over 1 year ago)
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.