Bug in struct module?

Skip Montanaro skip at mojam.com
Mon Aug 16 17:39:02 EDT 1999


    >>>> calcsize('hd')
    >> 16
    >>>> calcsize('dh')
    >> 10
    >> That seems like a bug to me.

Only if the native structs in C would be represented differently, and even
perhaps not then.  On my Linux box, the following short C program displays

    short size: 2
    double size: 8
    foo size: 12
    bar size: 12

when run.

    main() {
      struct {
	double f;
	short s;
      } foo;

      struct {
	short s;
	double f;
      } bar;

      printf("short size: %d\n", sizeof(short));
      printf("double size: %d\n", sizeof(double));
      printf("foo size: %d\n", sizeof(foo));
      printf("bar size: %d\n", sizeof(bar));
    }

It's possible that your compiler forces doubles onto eight-byte boundaries
but will truncate shorter fields at the end of structs.  On my machine it
appears to to pad everything to four-byte boundaries.  Compile and run the
program and see what you get.  Also, note that the struct module tries to
mimic the behavior of the C compiler.  From the struct module documentation.

    This module performs conversions between Python values and C structs
    represented as Python strings. ....

    By default, C numbers are represented in the machine's native format and
    byte order, and properly aligned by skipping pad bytes if necessary
    (according to the rules used by the C compiler).

That may well not be how VB does it, so you can't just assume that the
spacing the struct module gives you is how the VB object will be layed out.

Skip Montanaro	| http://www.mojam.com/
skip at mojam.com  | http://www.musi-cal.com/~skip/
847-971-7098






More information about the Python-list mailing list