Assuming short is 2 bytes and int is 4 bytes on a 32 bit OS. Is the following an undefined behavior?
short s = 42;
int *p = (int*)(&s);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, the code that you have posted does not exhibit undefined behavior but attempting to read
*pwould. Also, depending on the alignment requirements ofintandshort, the result of the cast may be unspecified and irreversable (see 5.2.10 [expr.reinterpret.cast] / 7).See ISO/IEC 14882:2011 3.10 [basic.lval] / 10:
The object that you are trying to access is a
shortand*pis a glvalue of typeintwhich doesn’t meet any of the above descriptions.