This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author meador.inge
Recipients eric.smith, mark.dickinson, meador.inge, sgk284
Date 2010-04-10.19:05:24
SpamBayes Score 6.2079515e-09
Marked as misclassified No
Message-id <1270926328.07.0.630994176901.issue7355@psf.upfronthosting.co.za>
In-reply-to
Content
> I'm half-convinced that struct.pack *should* ideally add trailing 
> padding in the same situation that C does, for consistency with C.  
> Then calcsize would match C's sizeof.

Maybe...  AFAIK, the C language does not mandate structure padding.
Section 6.7.2.1 of the C99 standard says that "unnamed padding" may be inserted anywhere inside of a structure object *except* at the beginning.  In other words, padding between members and trailing  padding is very implementation specific.  

When and why things are padded usually depends on the compiler, OS, and computer architecture.  For example, padding between members is usually
just an optimization for architectures like x86, but on architectures like ARM and MIPS you may end up with alignment faults.  Moreover, I 
think trailing padding is typically added to maintain alignment for 
arrays of structures, which again may be an optimization or correctness issue depending on the architecture.

So, I don't think we can match what C does since C leaves it open.
Perhaps we could match the C implementation that compiles the 'struct'
module, though?  Maybe there is a way we can compute the trailing padding in a fashion similar to how alignment is computed for various
types in the '*_ALIGN' macros in '_struct.c'?

Another thing that came to mind when thinking about structure layout issues was whether it might be useful to provide "packed" structures, e.g. no padding.  If the goal is to provide a way to read\write C-compatible structs, then currently there is no easy way to handle structures that have been serialized after being compiled with something like GCC's '-fpack-struct' option.
History
Date User Action Args
2010-04-10 19:05:28meador.ingesetrecipients: + meador.inge, mark.dickinson, eric.smith, sgk284
2010-04-10 19:05:28meador.ingesetmessageid: <1270926328.07.0.630994176901.issue7355@psf.upfronthosting.co.za>
2010-04-10 19:05:25meador.ingelinkissue7355 messages
2010-04-10 19:05:24meador.ingecreate