Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Explain why this prints the same
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Old Wolf, post: 2439477"] I don't think there is any lying here; you are allowed to cast one object pointer type to another as long as there are no alignment issues. The troubles only (potentially) start when you dereference the pointer. &a[0] must be correctly aligned for int, because the members of a[0] are ints, and there cannot be any padding before the first member of an array. Here there could be alignment issues. But if there are not, then no UB if c is not dereferenced. (Note: I don't think I worded that exactly right -- the code always has UB, but on implementations where there were no alignment problems, the code would behave in a well-defined manner). Agree (int* you mean, not *int). IMHO, *b causes undefined behaviour iff i >= 3, because "a" (ie. &a[0]) points to an array of 3 ints, and a bounds checking implementation could flag this. But if b were initialized as: int *b = &a; then there would be no UB because &a points to an object that is the size of nine ints. But some people think that even as written there is no UB because a[0] is part of the entire object "a", I don't recall seeing any consensus when this point has been debated in the past. *c would cause undefined behaviour in most cases, because either the bytes pointed to by c form a trap representation for an int*, or if they form an address that isn't the address of a valid object. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Explain why this prints the same
Top