+ 1

Reinterpret cast on different compiler

Hi 1.I had somewhere heard that reinterpret cast works based on compiler implementation. Is it correct ? 2. In my current code , output will be same on all the compiler... Right ? I think so because both class has two data members of same type... So in shallow copy, each member of one class is associated with the other member of different class. 3. What happens when my A class has only one member and Z has two members ? What is the case for vice versa i.e. A has two members and B has one member ? https://code.sololearn.com/c0W5NVu58DyF/?ref=app

25th Sep 2022, 8:21 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 odpowiedzi
+ 1
1. yes this is theoretically correct. Although it will have the same behaviour for most compilers in most scenarios. For this just think about what a reinterpret cast is. It is just treating they actual bits in memory as if they where some other type without any safety or checks. An int for example is only guaranteed to have a certain length, the actual size of an int is not defined in the standard, this could lead to compiler specific/undefined behaviour easily. 2. no, structs and classes might be padded for memory alignment reasons and other things that might make this statement invalid. It is simply undefined behaviour. The standard does not guarantee this by any means. But practically you are right that is should work on most compilers as expected, but this is not to be relied upon 3. undefinded behaviour :) sorry, for this anticlimactic answer, but that's it. You can ofc. see what happens when various compiler flags or different compilers in more detail on godbolt.org for example
27th Feb 2023, 9:43 AM
Erarnitox
Erarnitox - avatar
0
Thanks
27th Feb 2023, 9:59 AM
Ketan Lalcheta
Ketan Lalcheta - avatar