Given this field:
char lookup_ext[8192] = {0}; // Gets filled later
And this statement:
unsigned short *slt = (unsigned short*) lookup_ext;
What happens behind the scenes?
lookup_ext[1669] returns 67 = 0100 0011 (C), lookup_ext[1670] returns 78 = 0100 1110 (N) and lookup_ext[1671] returns 68 = 0100 0100 (D); yet slt[1670] returns 18273 = 0100 0111 0110 0001.
I’m trying to port this to C#, so besides an easy way out of this, I’m also wondering what really happens here. Been a while since I used C++ regularly.
Thanks!
The statement that you show doesn’t cast a char to an unsigned short, it casts a pointer to a char to a pointer to an unsigned short. This means that the usual arithmetic conversions of the pointed-to-data are not going to happen and that the underlying char data will just be interpreted as unsigned shorts when accessed through the
sltvariable.Note that
sizeof(unsigned short)is unlikely to be one, so thatslt[1670]won’t necessarily correspond tolookup_ext[1670]. It is more likely – if, say,sizeof(unsigned short)is two – to correspond tolookup_ext[3340]andlookup_ext[3341].Do you know why the original code is using this aliasing? If it’s not necessary, it might be worth trying to make the C++ code cleaner and verifying that the behaviour is unchanged before porting it.