c++ - Reliably determine the size of char -
i wondering how reliably determine size of character in portable way. afaik sizeof(char) can not used because yields 1, on system byte has 16 bit or more or less.
for example when dealing bits, need know how big is, wondering if code give real size of character, independent on compiler thinks of it. imo pointer has increased compiler correct size, should have correct value. right on this, or might there hidden problem pointer arithmetics, yield wrong results on systems?
int sizeofchar() { char *p = 0; p++; int size_of_char = (int)p; return size_of_char; }
i think it's not clear consider "right" (or "reliable", in title).
do consider "a byte 8 bits" right answer? if so, platform char_bit 16, of course answer computing:
const int octets_per_char = char_bit / 8; no need pointer trickery. also, trickery tricky:
on architecture 16 bits smallest addressable piece of memory, there 16 bits @ address 0x00001, another 16 bits @ address 0x0001, , on.
so, example compute result 1, since pointer incremented 0x0000 0x0001, doesn't seem expect compute.
1 use 16-bit address space brevity, makes addresses easier read.
Comments
Post a Comment