ubuntu@ubuntu:~$ ./a.out endianess: little try to access unaligned word equal to 0x1234: YES value: 0x1234 done
On Wed, Jul 29, 2015 at 3:55 PM, Dennis Luehring <dl.so...@gmx.net> wrote: > Am 29.07.2015 um 14:34 schrieb Karel Gardas: >> >> Once it boots, tell me how to find the asnwers to your >> questions. > > > compile with gcc test.cpp and run > > ----------- > #include <stdint.h> > #include <stdio.h> > #include <stddef.h> > #include <string.h> > #include <assert.h> > > int main() > { > uint16_t value = 0x1234; > > { > volatile uint8_t* ptr = (uint8_t*)&value; > printf("endianess: %s\n", ptr[0]==0x34 ? "little":"big"); > } > > uint8_t buffer[1+sizeof(value)]={0}; > uint8_t* ptr = buffer; > if(ptrdiff_t(ptr) % 2 == 0) > { > ++ptr; > } > uint16_t* unaligned_word = (uint16_t*)ptr; > > ::memcpy(unaligned_word, &value, sizeof(value)); > > printf("try to access unaligned word\n"); > uint16_t read = *unaligned_word; // here can happen Bus-Errors, > Exceptions, whatever your architecture likes > printf(" equal to 0x%04X: %s\n", value, read == value ? "YES":"!!NO!!"); > // sometimes you get the value - but its still wrong > printf(" value: 0x%04X\n", read); > printf("done\n"); > > return 0; > } > ----------- >