Topic: Portable in practice?


Author: comeau@panix.com (Greg Comeau)
Date: 1999/06/02
Raw View
In article <374D54DE.5A44@esva.net> spamless@esva.net writes:
>Given these structures:
>
>  struct A { char a[3]; };            // chars only
>  struct B { A m1; A m2; long m3; };  // mix of chars, long
>  struct C { A m1; A m2; };           // chars only
>
>The C++ Standard, 9.2 paragraph 12, permits adding alignment
>space after members, so it comes as no surprise that sizeof(B)
>is 12 for a common compiler and machine.
>
>Likewise, sizeof(C) doesn't have to be 6.  But it is very useful to use
>character only structs like C as buffers for communicating with the
>outside world, and that means depending on the compiler not inserting
>alignment.
>
>Is depending on sizeof(C)==6 portable in practice?  Are there any
>commercially important compilers or machines where sizeof(C)!=6 ?

Concepts like "practice" and "important" depends upon your
"programming universe".  If for some reason your must make this
assumption, you might want to add in code which can programmatically
detect when the assumption is not the case (and perhaps use some
other implementation innards)

- Greg
--
       Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
     Producers of Comeau C/C++ 4.2.38 -- New Release!  We now do Windows too.
    Email: comeau@comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
                *** WEB: http://www.comeaucomputing.com ***
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Bill Seymour <bsey@pobox.com>
Date: 1999/05/29
Raw View
Valentin Bonnard wrote:
>
> Beman Dawes wrote:
> >
> > Given these structures:
> >
> >   struct A { char a[3]; };            // chars only
> >   struct B { A m1; A m2; long m3; };  // mix of chars, long
> >   struct C { A m1; A m2; };           // chars only
> [...]
> > Is depending on sizeof(C)==6 portable in practice?  Are there any
> > commercially important compilers or machines where sizeof(C)!=6 ?
>
> I can see a good reason for this: copying 32 bits migth be
> easier than 24 bits (compare an int32 read/write to 3 chars
> access or a mask).
>

Also, an implementation might align anything with a type other
than char on some larger-than-char boundary.  (Note that m1 and
m2 are of type A, not char[3].)

Curiously, one implementation that helps to keep standards folk
honest--the Unisys A Series--is one on which this might actually
work, since sizeof(int) == 6. 8-)

--Bill Seymour
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Beman Dawes <spamless@esva.net>
Date: 1999/05/27
Raw View
Given these structures:

  struct A { char a[3]; };            // chars only
  struct B { A m1; A m2; long m3; };  // mix of chars, long
  struct C { A m1; A m2; };           // chars only

The C++ Standard, 9.2 paragraph 12, permits adding alignment
space after members, so it comes as no surprise that sizeof(B)
is 12 for a common compiler and machine.

Likewise, sizeof(C) doesn't have to be 6.  But it is very useful to use
character only structs like C as buffers for communicating with the
outside world, and that means depending on the compiler not inserting
alignment.

Is depending on sizeof(C)==6 portable in practice?  Are there any
commercially important compilers or machines where sizeof(C)!=6 ?

--Beman Dawes
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/05/27
Raw View
Beman Dawes wrote:
>
> Given these structures:
>
>   struct A { char a[3]; };            // chars only
>   struct B { A m1; A m2; long m3; };  // mix of chars, long
>   struct C { A m1; A m2; };           // chars only
[...]
> Is depending on sizeof(C)==6 portable in practice?  Are there any
> commercially important compilers or machines where sizeof(C)!=6 ?

I can see a good reason for this: copying 32 bits migth be
easier than 24 bits (compare an int32 read/write to 3 chars
access or a mask).

--

Valentin Bonnard


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: gbush@my-deja.com
Date: 1999/05/28
Raw View
In article <374D54DE.5A44@esva.net>,
  spamless@esva.net wrote:
> Is depending on sizeof(C)==6 portable in practice?  Are there any
> commercially important compilers or machines where sizeof(C)!=6 ?
I expect most compilers on 32-bit platforms to have 4 bytes default
alignment. So the sizeof(C) will be 8. At the same time (at least some)
compilers allow to specify the alignment using #pragma and/or comand-
line keys and options.
Gene.


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]