Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c++ Subject: Re: [QT creator on "nix"] - getting a strict 8 bit (1 byte) array with no padding Date: Thu, 28 Nov 2019 13:21:43 -0800 Organization: A noiseless patient Spider Lines: 72 Message-ID: <86lfrzae8o.fsf@linuxsc.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="dce185ee50ea005ee4a34884a21fb89a"; logging-data="29801"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Alv0X77jzvM2D0H2sn3nSLSJ+bfC95ak=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:kqmYiY4gYZEY1jznOVQoykY1uM8= sha1:zqnbx43WkiIyQ3POfFEb2keI4oE= Xref: csiph.com comp.lang.c++:70094 Soviet_Mario writes: > I am trying to create a statically linked library in the said IDE. > I would not want to include many QT features for now > so, what is the STANDARD C++ way to get > unsigned 8 bits integer type ? > > I fear that unsigned char is system dependent (and even if there are > "wider" explicitely supported types, no plain char type seems to > warranty to be 8 bits wide). > > The solution of creating an unnamed structure with a "bit field like > struct {unsigned Pixel : 8} Mask; > gives me a warning like : 3 byte PADDING used to align the struct > Mask. > > Now I don't know whether this padding would exist also in ARRAY > dynamic allocation (I'm not speaking for just the first address of the > array, but for EVERY SINGLE ITEM in it, wasting as much as 3 times the > space required !) > > How to get rid of such alignment feature ? > > in Visual Studio there were align / pad directives, but here on QT I > dunno anything. > > I don't care a lot about fastness of access, but need a true compact > byte array (QT has its own, but I will need to communicate with a > Gambas program using the static library, so I'd be not happy to > interface QT internals with Gambas, I'd prefer a standard C++ > types-based solution). > > long ago CHAR was 8 bit wide, but now it seems to be platform > dependent, and more often "a word" wide. > > QT has its explicitely sized integers (even if no 8-bit sized) I would offer an alternate set of suggestions. First, pick a name for the type you will use in your code. For example, in this case, you might pick 'GamByte' as the type name you will be using. Second, see if the library being used (Gambas) supplies a name for its type in one of its headers. If it does, define your type name in terms of that. These names might even be the same, except they might be declared in different namespaces. Third, if the library being used does not supply a name for the type it is using, but just spells out the concrete type in its function declarations, etc, define your type name as that, eg, using GamByte = unsigned char; // for use with Gambas where 'unsigned char' is meant to be the type given in Gambas header files, assuming there is one. Fourth, if the library being used does NOT have the type in question appear in any of its header files, use 'unsigned char'. If it's important to guarantee that the type is exactly eight bits, use a static assertion that CHAR_BIT == 8. The reason is that 'unsigned char' is guaranteed to work, if any type is, and if for any reason it doesn't work then the error message produced by the static assertion will be more easily understood than, eg, a syntax error trying to use a non-existent uint8_t. Fifth, if you never need individual 8-bit bytes in isolation, but only in aggregates (arrays or array-like), you might consider defining the aggregate type as a struct inside a template, which externally could look like an array of 8-bit types even though internally it might be, eg, 32-bit unsigned words used to hold four 8-bit values.