Answer the question
In order to leave comments, you need to log in
Is data represented as bits in cells from right to left or from left to right?
In other words, the lower bits in the cells go to the left or right
#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
setlocale(0, "");
long x = 20;
long *pi = &x;
short *ps = reinterpret_cast<short*>(&x) + 1;
cout
<< (static_cast<void*>(pi) == ps) << '\n';
cout
<< pi << '\n'
<< ps << '\n'
<< *pi << '\n'
<< *ps << '\n';
return 0;
}
0 // false
0x3bfdcc
0x3bfdce
20
0
Answer the question
In order to leave comments, you need to log in
The correct answer is neither. No way. Compare pointers not from the same array - undefined behavior. It was recently in an article on Habré. They refer to section 3.3.8, "Relational operators".
Moreover, you still have UB in your program: you cannot access data in a long variable through a short pointer - This is a violation of strict aliasing.
Thus, your program can output anything, depending on the compiler version and settings.
In practice, there are no left and right there, there are only minor and major addresses, read about big / little endian byte orders. They are there and so and so in different architectures. On x86, for example, the low byte has the lower address.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question