Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #70062

Re: [QT creator on "nix"] - getting a strict 8 bit (1 byte) array with no padding

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c++
Subject Re: [QT creator on "nix"] - getting a strict 8 bit (1 byte) array with no padding
Date 2019-11-26 08:22 +0100
Organization A noiseless patient Spider
Message-ID <qrijrn$c5c$1@dont-email.me> (permalink)
References (10 earlier) <qr8pu5$657$1@dont-email.me> <qr99qg$ats$1@dont-email.me> <qren31$fs3$1@dont-email.me> <qreqkn$51v$1@dont-email.me> <qrh5tl$aee$1@dont-email.me>

Show all headers | View raw


On 25/11/2019 19:18, James Kuyper wrote:
> On 11/24/19 3:53 PM, Soviet_Mario wrote:
>> On 24/11/2019 20:53, David Brown wrote:
> ...

> ...
>>> Or perhaps you think compilers add random padding and
>>> re-arrange structs in odd and unpredictable ways.
> ...
>> I'm not sure of the degree of freedom they have in
>> "reordering" members though.
> 
> "Non-static data members of a (non-union) class with the same access
> control (Clause 14) are allocated so that later members have higher
> addresses within a class object." (12.2p17).
> 
> Therefore, implementations have the freedom to reorder class members
> only if you explicitly give them that freedom by using access control
> specifiers.

Just to add a touch of confusion here - implementations can reorder 
class or struct members any way they want, as long as it is invisible to 
the code.  gcc had an optimisation that did this for a while, 
re-arranging structs for better packing or more efficient access in 
code.  But it could only do that for very "local" structs - nothing that 
was passed around much, or where the layout could conceivably have been 
visible or dictated by external requirements.  It was removed before 
long, however, because it was rarely possible for the compiler to make 
such re-arrangements safely and the code was complicated to maintain. 
(It is just another example of the "as if" rule - compilers can do what 
they want with the code, as long as it works "as if" it had followed all 
the rules slavishly.)  Needless to say, this re-arranging would not have 
occurred for the kind of code under discussion in this thread, since the 
order matters here.

> 
>> Sometimes in constructors I get some warning about the
>> members will be initializated in a different order than
>> declared.
> 
> That could happen through the use of access control specifiers.
> 

I don't think that is it.  The "-Wreorder" warning in gcc, enabled by 
"-Wall", gives you a warning if the order of initialisers does not match 
the order of the members:

struct A {
   int i;
   int j;
   A(): j (0), i (1) { }
};

That is because the compiler will actually initialise "i" first, then 
"j" (I think the standards say this ordering is unspecified), and it 
warns you that the order does not match what you wrote in the code.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: [QT creator on "nix"] - getting a strict 8 bit (1 byte) array with no padding Soviet_Mario <SovietMario@CCCP.MIR> - 2019-11-24 21:53 +0100
  Re: [QT creator on "nix"] - getting a strict 8 bit (1 byte) array with no padding Öö Tiib <ootiib@hot.ee> - 2019-11-24 13:46 -0800
    Re: [QT creator on "nix"] - getting a strict 8 bit (1 byte) array with no padding Soviet_Mario <SovietMario@CCCP.MIR> - 2019-11-25 12:44 +0100
      Re: [QT creator on "nix"] - getting a strict 8 bit (1 byte) array with no padding Öö Tiib <ootiib@hot.ee> - 2019-11-25 06:55 -0800
  Re: [QT creator on "nix"] - getting a strict 8 bit (1 byte) array with no padding James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-25 13:18 -0500
    Re: [QT creator on "nix"] - getting a strict 8 bit (1 byte) array with no padding David Brown <david.brown@hesbynett.no> - 2019-11-26 08:22 +0100
      Re: [QT creator on "nix"] - getting a strict 8 bit (1 byte) array with no padding Paavo Helde <myfirstname@osa.pri.ee> - 2019-11-26 10:19 +0200
        Re: [QT creator on "nix"] - getting a strict 8 bit (1 byte) array with no padding David Brown <david.brown@hesbynett.no> - 2019-11-26 10:15 +0100
      Re: [QT creator on "nix"] - getting a strict 8 bit (1 byte) array with no padding James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-26 11:59 -0500

csiph-web