would using casts fix the problem?
would using casts fix the problem?
Posted Apr 16, 2008 20:27 UTC (Wed) by jengelh (guest, #33263)In reply to: would using casts fix the problem? by MathFox
Parent article: GCC and pointer overflows
Or better yet don't cast at all. %td
for printf
can take a ptrdiff_t
, which is what such a subtraction should usually yield.
Posted Apr 16, 2008 21:29 UTC (Wed)
by MathFox (guest, #6104)
[Link] (1 responses)
Standard conforming programs don't rely on pointer layout.
Posted Apr 17, 2008 18:55 UTC (Thu)
by hummassa (subscriber, #307)
[Link]
I guess that you missed the point completely. aend-a (no casts needed) should give 10 on any platform. The bit pattern in a pointer is implementation defined and that particular platform had (64-bits IIRC) word-based addresses in a 48 bit address space. The low 48 bits of a character pointer were the address of the word containing the character; the high bits addressed the byte in the word.
would using casts fix the problem?
would using casts fix the problem?
yes,
aend - a
(without cast) _is_ 10 in any platform, but
(int)aend - (int)a
is _undefined_ behaviour, because the bit pattern when converting from
pointer to int is implementation-defined.
Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds