Path: csiph.com!weretis.net!feeder6.news.weretis.net!feeder7.news.weretis.net!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++ Subject: Re: [QT creator on "nix"] - getting a strict 8 bit (1 byte) array with no padding Date: Fri, 22 Nov 2019 22:04:23 -0800 Organization: None to speak of Lines: 67 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="dfad491d1778b0c95e43bf538d0f4f21"; logging-data="14872"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX180698UHPGapUqBNILSdes9" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:MPu1OoV6fhiWnyKQMsgEQRvTrsY= sha1:qPIa9tJg+4TcDei7z4wsL2WIEbU= Xref: csiph.com comp.lang.c++:70006 Soviet_Mario writes: [...] > let's say it explicitely > > if I have > union > { > struct > { > u_int_8 Red, Green, Blue, Alpha; > }; > u_int_32 AllChannels; > }; > > without PRAGMA, do I have any explicit or implicit warranty > that my union be 32 bit long ? If so, PRAGMA is useless. If > not, it seems it might be useful. Fixing the spelling of uintN_t (n=8,32) and adding a tag to the union so you can use it to define objects: union u { struct { uint8_t Red, Green, Blue, Alpha; }; uint32_t AllChannels; }; (You have an anonymous struct, which is standard but wasn't added until C11, so older compilers might not support it, or might not support it by default.) You're guaranteed that the Red and AllChannels members start at the same location, the very beginning of the union. You're guaranteed that Red, Green, Blue, and Alpha are at increasing offsets in the order you defined them. It's very very likely that the struct and the union will both be exactly 32 bits. A compiler is not prohibited from adding padding between members or after the last one, but there's unlikely to be any good reason to do so in this case. The most likely way this could fail is if a compiler insisted on making the structure 64 bits. You can easily add a compile-time or run-time assertion that sizeof (struct u) == 4 && CHAR_BIT == 8 so your program won't compile, or at least won't run, in the unlikely event that it's used with an implementation that doesn't meet your requirements. There is *no* guarantee about which of the four uint8_t members correspond to which part of AllChannels. Google "endianness" if you're not familiar with this. It's very common for Red to overlap either the high-order byte or the low-order byte of AllChannels. The other 22 permutations are rare or nonexistent, but Google "PDP-11 middle-endian" for an odd case. If you're doing bitwise operations on the entire 32-bit word without caring about the order of the bytes, then that might not be an issue. Attempting to use #pragma pack is very likely to do nothing *at best*. [...] -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for Philips Healthcare void Void(void) { Void(); } /* The recursive call of the void */