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.

Comments on Cast uninitialized variable to (void)

Parent

Cast uninitialized variable to (void)

+3
−0

Is it undefined behaviour to cast an uninitialized variable to (void)?

Example:

int main()
{
  int x;
  (void)x;
  return 0;
}
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

2 comment threads

What are you trying to achieve? (3 comments)
I wonder if I should also tag this C++ also. The answer *might* be different for these different lang... (3 comments)
Post
+5
−0

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:

{
  volatile int x;
  (void)x; // undefined behavior, lvalue access of local uninitialized variable
}

While this is ok:

{
  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? 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:

{
  int x,y;
  (void)(x=y); // undefined behavior, lvalue access of local uninitialized variable y
}
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Excellent answer which shows how easy it is to get the standard wrong, as I did. (1 comment)
Excellent answer which shows how easy it is to get the standard wrong, as I did.
Estela‭ wrote about 2 years ago

Excellent answer which shows how easy it is to get the standard wrong, as I did.