Nested structs and struct.pack 
Author Message
 Nested structs and struct.pack

Hi!

I'm having problems with nested C structs and the
struct module. The size of the structs are
different for python and C. The following
describes the problem:

// C
#include <stdio.h>

struct a
{
        long a;

Quote:
};

typedef struct a A;

struct b
{
        A a;
        char c;

Quote:
};

typedef struct b B;

struct c
{
        B b;
        char c;

Quote:
};

typedef struct c C;

int main(int argc, char* argv[])
{
        printf("Type sizes\n");
        printf("A    : %u\n", sizeof(A));
        printf("B    : %u\n", sizeof(B));
        printf("C    : %u\n", sizeof(C));

Quote:
}

// end

Here the size of C is 12.

# Python

import struct
struct.calcsize("lcc")
# end

Here the size is 8.

I've probably misunderstood how the compiler lays
out nested structs. But is there any way to tell
the struct module that I have nested structs so
that the C compiler and Python produce the same
result ?

With regards,
-Ken.

Sent via Deja.com
http://www.*-*-*.com/



Sun, 22 Jun 2003 20:38:29 GMT  
 Nested structs and struct.pack

Quote:
>I've probably misunderstood how the compiler lays
>out nested structs. But is there any way to tell
>the struct module that I have nested structs so
>that the C compiler and Python produce the same
>result ?

You can use pad bytes ('x' in the format string). So instead of a format of
"lcc" you could use "lcxxxcxxx" if your machine is little endian, or
"lxxxcxxxc" if it is big endian.

Of course you will have to know exactly how the struct is laid out in
memory and this will vary between machines, compilers, or even with
different options given to the same compiler.



Sun, 22 Jun 2003 21:51:29 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. struct module + array module = ? ( reading C structs )

2. no struct.pack for unicode strings?

3. packing (struct module)

4. Bug in struct.pack() for Sparc Solaris 2.3?

5. portability problem with struct.{pack,unpack}

6. Need/wish for a 'commutating' inversion between struct.pack and struct.unpack

7. C arrays, J structs, and LinkJ

8. REALBasic newbie question: structs and pointers?

9. C structs in rb?

10. Q: Creating instance of a struct in VisualAge C Interface

11. C4: Radio button selection in menu struct (instead of check boxes)

12. Getting array of structs back from C++ function (reviewed)

 

 
Powered by phpBB® Forum Software