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

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

Well, an "uninitialized" variable is, in fact, not uninitialized as that terminology may suggest; it is just filled with random garbage that happened to be on that memory section. That is to say, u...

posted 1y ago by tmpod‭  ·  edited 1y ago by tmpod‭

Answer
#2: Post edited by user avatar tmpod‭ · 2022-09-09T23:46:25Z (over 1 year ago)
Language improvements
  • Well, an "uninitialized" variable is, in fact, not uninitialized as that terminology may suggest; it is just filled with random garbage that happened to be on that memory section. That is to say, uninitialized variables are not special, they just happen to not contain any meaningful value, but they do have a value.
  • Now, regarding the casting situation: `void` is not a type, rather the contrary -- it's the absence of type. Knowing this, casting to it wouldn't make much sense, but as it happens, it is allowed, yet simply as a means to discard an expression.¹²
  • Now, casting to a void pointer (`void*`) is another thing, and it's mostly used as a sort of "generics" in C (for example, the standard `qsort` function uses this). Since these pointers are aligned as char ones³ and can be cast to any other pointer type, you can write functions that take void pointers and inside cast them to their concrete types, thus allowing a generic-like function signature (naturally, a whole lot less safe than what you may find in other languages).
  • Null pointers are essentially void pointers to an integer constant expression of value 0 and have the interesting property of being guaranteed that when cast to concrete pointer types, they will never be considered equal.⁴
  • Hope this answers your question, if not let me know in the comments :)
  • ---
  • ¹ From C's standard, section 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.)
  • ² From C++'s standard, section [5.2.9.4](http://www.csci.csusb.edu/dick/c++std/cd2/expr.html):
  • > Any expression can be explicitly converted to type _cv_ void." The expression value is discarded.
  • ³ From C's standard, section 6.3.2.3:
  • > A pointer to void may be converted to or from a pointer to any incomplete or object type. A pointer to any incomplete or object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
  • And 6.2.5.27:
  • > A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.
  • ⁴ From C's standard, section 6.3.2.3:
  • > An integer constant expression with the value 0, or such an expression cast to type `void *`, is called a _null pointer constant_. If a null pointer constant is converted to a pointer type, the resulting pointer, called a _null pointer_, is guaranteed to compare unequal to a pointer to any object or function.
  • Well, an "uninitialized" variable is, in fact, not uninitialized as that terminology may suggest; it is just filled with random garbage that happened to be on that memory section. That is to say, uninitialized variables are not special, they just happen to not contain any meaningful value, but they do have a value.
  • `void` is not a type, rather the contrary -- it's the absence of type. Knowing this, casting to it wouldn't make much sense but as it happens, it is allowed; it simply discards the expression being cast.¹²
  • Now, casting to a void pointer (`void*`) is another thing. It's mostly used as a sort of "generics" in C (e.g, the standard `qsort` function uses this). Since these pointers are aligned as character ones³ and can be cast to any other pointer type, you can write functions that take void pointers but cast them to their concrete types inside, thus allowing a generic-like function signature (naturally, a whole lot less safe than what you may find in other languages).
  • Null pointers are essentially void pointers to an integer constant expression of value 0 and have the interesting property of being guaranteed that when cast to concrete pointer types, they will never be considered equal.⁴
  • Hope this answers your question, if not let me know in the comments :)
  • ---
  • ¹ From C's standard, section 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.)
  • ² From C++'s standard, section [5.2.9.4](http://www.csci.csusb.edu/dick/c++std/cd2/expr.html):
  • > Any expression can be explicitly converted to type _cv_ void." The expression value is discarded.
  • ³ From C's standard, section 6.3.2.3:
  • > A pointer to void may be converted to or from a pointer to any incomplete or object type. A pointer to any incomplete or object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
  • And 6.2.5.27:
  • > A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.
  • ⁴ From C's standard, section 6.3.2.3:
  • > An integer constant expression with the value 0, or such an expression cast to type `void *`, is called a _null pointer constant_. If a null pointer constant is converted to a pointer type, the resulting pointer, called a _null pointer_, is guaranteed to compare unequal to a pointer to any object or function.
#1: Initial revision by user avatar tmpod‭ · 2022-09-09T23:40:05Z (over 1 year ago)
Well, an "uninitialized" variable is, in fact, not uninitialized as that terminology may suggest; it is just filled with random garbage that happened to be on that memory section. That is to say, uninitialized variables are not special, they just happen to not contain any meaningful value, but they do have a value.

Now, regarding the casting situation: `void` is not a type, rather the contrary -- it's the absence of type. Knowing this, casting to it wouldn't make much sense, but as it happens, it is allowed, yet simply as a means to discard an expression.¹²

Now, casting to a void pointer (`void*`) is another thing, and it's mostly used as a sort of "generics" in C (for example, the standard `qsort` function uses this). Since these pointers are aligned as char ones³ and can be cast to any other pointer type, you can write functions that take void pointers and inside cast them to their concrete types, thus allowing a generic-like function signature (naturally, a whole lot less safe than what you may find in other languages).

Null pointers are essentially void pointers to an integer constant expression of value 0 and have the interesting property of being guaranteed that when cast to concrete pointer types, they will never be considered equal.⁴

Hope this answers your question, if not let me know in the comments :)

---

¹ From C's standard, section 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.)

² From C++'s standard, section [5.2.9.4](http://www.csci.csusb.edu/dick/c++std/cd2/expr.html):

> Any expression can be explicitly converted to type _cv_ void." The expression value is discarded.

³ From C's standard, section 6.3.2.3:

> A pointer to void may be converted to or from a pointer to any incomplete or object type. A pointer to any incomplete or object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

And 6.2.5.27:

> A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.

⁴ From C's standard, section 6.3.2.3:

> An integer constant expression with the value 0, or such an expression cast to type `void *`, is called a _null pointer constant_. If a null pointer constant is converted to a pointer type, the resulting pointer, called a _null pointer_, is guaranteed to compare unequal to a pointer to any object or function.