Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!lupine!rfg From: rfg@NCD.COM (Ron Guilmette) Newsgroups: comp.lang.c++ Subject: Re: Byte padding question Message-ID: <1477@lupine.NCD.COM> Date: 7 Sep 90 21:51:15 GMT References: Distribution: comp Organization: Network Computing Devices, Inc., Mt. View, CA Lines: 49 In article keving@r4.uk.ac.man.cs (Kevin Glynn) writes: > > > When building a class hierarchy in C++ we found that some >extraneous char's were being inserted into the C structure produced by >the AT&T front end. After writing a fairly large test program it was >found that these bytes were only being generated (at least in our >tests) when an empty class (methods only, no member variables) was the >base class. I also noticed that in it's child class the extra byte >generated for the empty base class was added as well as a new char >array of size 1. > > In the work our department is doing, this will cause problems in >the future and we would like to clear up the reason why this is >happening (and even better how to stop it happening!). Well, you can't stop it. As I recollect, the reason that it is happening is that Bjarne though that it would be a good idea to insure that each element of each array in any given program should have a unique address. Obviously, given: struct s {} array[20]; if the elements are given zero length then: &array[1] == &array[2] I can't tell you precisely why Bjarne though it would be important to insure that array elements all had unique addresses (because I don't know for sure and because I don't have my copy of E&S handy), but I'm willing to bet that he found some very good reason for it. (Note that in ANSI C, it is illegal to even declare a struct or union type which has no members, so in ANSI C this issue never even comes up.) Regarding class types derived (singly) from `empty' class/struct types, the compiler can't simply forget about the (artificial 1 byte) size of the base type. That would cause problems for the compiler when assignments like: *base_pointer = *derived_pointer; were executed. -- // Ron Guilmette - C++ Entomologist // Internet: rfg@ncd.com uucp: ...uunet!lupine!rfg // Motto: If it sticks, force it. If it breaks, it needed replacing anyway.