Top
Best
New

Posted by germanrabbit 2 days ago

What's the difference between an integer and a pointer? (2018)(blog.regehr.org)
69 points | 39 commentspage 2
m00dy 2 hours ago|
range of values
cowlevel 2 hours ago||
> First, does this C program execute undefined behavior? It does not! Computing out-of-bounds pointers is UB but we are free to do whatever we like with integer-typed values that were derived from pointers. Second, does the C standard mandate the behavior that we saw here? It does not!

Behaviour that is not defined is undefined behaviour. Integer to pointer conversion is IDB, which is the same as UB except that a conforming compiler documents what could happen. I believe it's typically documented as unpredictable, so the same as plain UB.

tsimionescu 2 hours ago||
There is a vast difference between IDB and UB. IDB is perfectly fine to rely on for your compiler, but it means your program may not be portable to other compilers.

UB means your program is not a C program and has no semantics whatsoever past the point it reaches the UB. The compiler is thus free to assume that the point that the UB would happen can't be reached and optimize in accordance to that assumption - which many modern compilers do, with often unexpected consequences (such as the infamous case of removing an integer overflow check in Linux because an_int+1>an_int is guaranteed to be true in the C abstract model).

cowlevel 2 hours ago|||
How does gcc document integer to pointer conversion?
Maxatar 1 hour ago||
The rules and process are defined here:

https://gcc.gnu.org/onlinedocs/gcc/Arrays-and-pointers-imple...

cowlevel 1 hour ago||
> When casting from pointer to integer and back again, the resulting pointer must reference the same object as the original pointer, otherwise the behavior is undefined.

So I'm right. This is implementation-defined, and the way the implementation has defined it is to make it fully UB (except for round trips).

sham1 38 minutes ago||
Whenever something is undefined behaviour or not is a matter of the standard and what the implementation may or may not have defined for that is immaterial to that. As far as the standard is considered, when they encounter UB, you no longer have a valid C program. However the compiler can still choose to do something with it, which in this particular case manifests as GCC has documented it, but some other compiler can do something else for it.
Maxatar 1 hour ago|||
[dead]
saagarjha 2 hours ago||
No, these are different things.
imnotr0b0t 1 hour ago|
This is interesting