r/C_Programming • u/green_krokodile • Jul 29 '26
Question Is cast from char** to void** undefined behaviour?
Hi, all the AI models tell me that cast from char** to void** undefined behaviour, is it really UB? thanks
0
Upvotes
2
u/TheChief275 Jul 29 '26 edited Jul 29 '26
you are allowed to cast from any pointer type to any other pointer type, however, since C99 you are not allowed to dereference due to strict aliasing. the LLMs are probably assuming you plan to dereference (or you're already doing that).
you are only allowed to dereference pointers cast to and from void *, and char *. this means you are allowed to cast your char ** to a void *, pass it to a function, and the function taking a void * would be allowed to assume it received a void ** (casting void * to void ** and dereferencing).
(note that the casting rules around function pointer types are slightly different, as it isn't guaranteed that e.g. sizeof(void *) >= sizeof(void (*)(void)))
this limit is only for pointer types; if you can dereference a concrete object representation, you are allowed to "pun cast" the bits to a different representation. a classic example use case can be found in the iconic Quake III Arena Q_rsqrt function:
but this is technically the same strict aliasing violation we talked about before. you can do this safely without UB, by utilizing a "pun_cast":
where PUN_CAST is defined like so:
(the use of typeof is not required and is only for convenience and syntactic correctness of declarations and casts; you can pass the From type explicitly, and typedef any type that has a different order in usage before passing to the macro (e.g. void (*)(void) f (incorrect), and void (*f)(void) (correct)))
you can even make this compatible with C++, but it requires some more work as the union trick is actually UB in C++. instead, you can use std::memcpy:
(for C++20 and beyond, there is actually a built-in std::bit_cast for this exact purpose. however, I like to write a slight extended version that actually allows sizeof(To) >= sizeof(From), zeroing out the bytes that aren't in from. additionally, you might want to insert static assertions that To and From aren't pointer types to prevent invalid usage, as again, that would just lead to strict aliasing violations)
21
u/McDutchie Jul 29 '26
Stop using AI, it's literally a bullshit generator. Do the learning yourself.
4
u/tavianator Jul 29 '26
It is wrong to think of strict aliasing as making casts UB. A more accurate mental model is that every object has a "dynamic" type, and when you read or write an object, you have to use the real type, or a "compatible" one.
For example:
Now to your question:
To know for sure what's allowed and what's not, you have to check the C standard for the definition of "compatible type" but roughly, you're allowed to differ in qualifiers and signedness (so you can read/write an
intas anconst unsigned intbut not along).There's a special exception for
charso that you may read/write any type as achar. It does not work the other way around: you cannot for example access achar[4]as if it were anint.Finally, compatibility does not transcend through pointer types, so while
intis compatible withunsigned int,int *is not compatible withunsigned int *.10
u/Wild_Meeting1428 Jul 29 '26 edited Jul 29 '26
No, a reinterpreting cast is never UB, only operations on the cast result may be UB. In this case, you are even allowed to do arithmetic operations on both pointers and both will yield the same result.
5
u/sciencekm Jul 29 '26
Both are pointers (to pointers), which is fine. Somewhere, you will have to use that void**, and you just need to remember that it is actually a char**.
1
u/Interesting_Buy_3969 Jul 29 '26
The cast per se isn't UB; it's all about that it can easily become UB if you use the obtained result incorrectly, i.e. if you try dereferencing it.
1
u/flatfinger Jul 29 '26
The Standard specifies that
char*andvoid*must use compatible representations. It also, however, gives implementations broad latitude to perform optimizing transforms that will handle many otherwise-defined corner cases incorrectly(*). Outside of a few cases for which the Standard mandates correct behavior, the question of what corner cases an implementation will handle correctly is a quality-of-implementation matter over which the Standard waives jurisdiction.(*) The published Rationale makes clear that the authors recognized that in the absence of such allowance, such transforms would be "incorrect", using that word.
When clang and gcc are invoked with the
-fno-strict-aliasingflag, they will by specification correctly handle corner cases where the representation of achar*is accessed by dereferencing avoid**, or vice versa. Absent that flag, they should not be relied upon to do so.Note that on most platforms, all pointer-to-data types use the same representation, but there are some platforms where
int*andvoid*use different representations (other types will generally use a representation matching one of those). Most such platforms are best programmed with code written very deliberately for them, so portability of other code to such platforms should generally not be a concern.1
u/Heroes_Of_Balkan Aug 07 '26
I'm curious what code snipped makes the AI mark it as UB. I'm also curious why would you cast `char**` to `void**`? I'd cast in reverse. By casting to `void**`, you lose some information about your data's type. If the function expects a `void**` parameter, any `T**` parameter (where `T` is your type from pointer pointer) can be passed without casting
According to what user tstanisl gave (link to his comment: https://www.reddit.com/r/C_Programming/comments/1v9ritr/comment/p0gf52x/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button ), the compiler may or may not treat `void**` and `char**` as the same. Be careful how you handle your data. Other than that, I can only guess what is the issue in your code
What would be helpful is to ask your AI model to elaborate why it thinks it's an UB. From my experiences (a total of ~50-ish prompts in a span of 3-4 years), AI models can often be wrong. It's important to not trust everything it says and fact-check what it claims. It may be right, it may not. You can try prompting "Why do you think this is UB?"
1
u/HashDefTrueFalse Jul 29 '26
Not UB. However you probably want void* (one *) as a pointer is a pointer if you don't know what it points to.
1
u/tstanisl Jul 29 '26
Very technically and strictly speaking AI is correct.
Rules about representation of pointer types and their alignment requirements can be found at 6.2.5p28:
The cast of
char**tovoid**falls to the last bullet point so on some esoteric platform such a cast could be UB.0
u/zhivago Jul 29 '26
They're incompatible types.
Conversion between them is a constraint violation.
Your program is no longer strictly conforming.
The result of the cast may be undefined.
0
u/This_Growth2898 Jul 29 '26
No, it is not.
Please provide some context for your question. What are you trying to achieve? What is your code? I understand you want to keep it short, but stating the actual problem behind the question is needed to answer you properly.
Also, saying you've consulted the AI doesn't add much, besides the fact you didn't ask the AI to provide links to the C Standard to support its claims (which is bad), but at least you've tried to find the answer before asking here (which is good).
0
u/Living_Fig_6386 Jul 29 '26
No. Pointer casts in C are never undefined behavior. It may make for hard to follow code, depending on what you are doing, but it's fine.
0
u/[deleted] Jul 29 '26
[deleted] โ view deleted comment