Reputation: 23344
I am reading the code for _start as packaged with FreeBSD, and am curious about something particular I'm seeing. Line 61 casts a char** to a void* and then immediately to a long*. I've seen a similar StackOverflow question, but it doesn't apply here because all pointers are the same size. Can anyone tell me why this line casts twice instead of just once?
Upvotes: 3
Views: 134
Reputation: 9204
ISO c99 : 6.3.2.3 Pointers
1
" 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."
7
" A pointer to an object or incomplete type may be converted to a pointer to a different
object or incomplete type. If the resulting pointer is not correctly aligned for the
pointed-to type, the behavior is undefined
. Otherwise, when converted back again, the
result shall compare equal to the original pointer. When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte of the object. Successive increments of the result, up to the size of the object, yield pointers to the remaining bytes of the object."
2nd paragraph says that you can convert any pointer to one type to other type but if resulting pointer is not aligned then behaviour is undefined
.
While 1st paragraph doesn't say any thing about undefined behaviour
.
So I think converting void *
to any type is more safe method than directly converting from one type to another.
Hence you are seeing the same.
EDIT : I don't think it's the exact answer of your question but at least you can see the related stuff in the c99 standard as mentioned above.
Upvotes: 1