467,885 Members | 1,198 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,885 developers. It's quick & easy.

A bug in struct module on the 64-bit platform?

Hi,

I have a user who complained about how "struct" module computes C
struct data size on Itanium2 based 64-bit machine.

His first reproducer was
--------------------------------------
#!/usr/local/bin/python
import struct
fmthead = '12id5i5d7id5i3di12i3di'
fmtsize = struct.calcsize(fmthead)
print fmthead,fmtsize
--------------------------------------
And it prints 12id5i5d7id5i3di12i3di 292

And he further provided
struct.calcsize('idi') 16struct.calcsize('idid') 24struct.calcsize('did')

20

In response to those, I created corresponding C struct and computed the
data size using "sizeof" operation (gcc was used for compilation), but
they don't seem to match.

Is this a known problem?

Best,
-Dong

Dec 1 '05 #1
  • viewed: 1542
Share:
4 Replies
ah**@llnl.gov wrote:
Hi,

I have a user who complained about how "struct" module computes C
struct data size on Itanium2 based 64-bit machine.


I wouldn't be surprised, but I don't understand the problem.
>>struct.calcsize('idi') 16 >>struct.calcsize('idid') 24 >>struct.calcsize('did') 20


These are what I would expect on a 32 or 64 bit platform. i == int, d
== float. ints are typically 4 bytes on 64 bit platforms. If you want
8 byte integers, you typically need to use longs (format letter is ell,
l).

You didn't say which version of python, so it's possible this was a bug
that was fixed too.

On my system:

python: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for
GNU/Linux 2.4.1, dynamically linked (uses shared libs), not stripped
struct.calcsize('l') #that's a lowercase ell

8

If you think it's a bug, you should file a bug report on source forge.

n

Dec 1 '05 #2
I'm guessing that the expected behavior is
struct.calcsize('idi')

20
because the double should be aligned to an 8-byte boundary.
This is the case on my linux/x86_64 machine:
$ python -c 'import struct; print struct.calcsize("idi")'
20
I don't know much about 'itanium', but i'd be surprised if they
chose 4-byte alignment for doubles.

http://h21007.www2.hp.com/dspp/tech/...01,180,00.html

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDjvpLJd01MZaTXX0RAucFAJ0UKtJ4OksxIPVIELYGl5 T8wXGYNACeKvIo
DvQaqZW/XMm3dq7lL/j9j5g=
=CusK
-----END PGP SIGNATURE-----

Dec 1 '05 #3
Neal Norwitz wrote:
I have a user who complained about how "struct" module computes C
struct data size on Itanium2 based 64-bit machine.


I wouldn't be surprised, but I don't understand the problem.
>>struct.calcsize('idi')

16
>>struct.calcsize('idid')

24
>>struct.calcsize('did')

20


These are what I would expect on a 32 or 64 bit platform. i == int, d
== float. ints are typically 4 bytes on 64 bit platforms. If you want
8 byte integers, you typically need to use longs (format letter is ell,
l).


except that the 64-bit double should, on most 64-bit platforms, have
64-bit alignment, so

struct.calcsize('idi')

should be 4 bytes integer plus 4 bytes padding plus 8 bytes double plus
4 bytes integer, or 20 bytes.

I'd expect
struct.calcsize('idi') 20struct.calcsize('idid') 32struct.calcsize('did')

24

but I have no 64-bit box within reach just now...

or isn't "native alignment" the default?

</F>

Dec 1 '05 #4
je****@unpythonic.net wrote:
This is the case on my linux/x86_64 machine:
$ python -c 'import struct; print struct.calcsize("idi")'
20
I don't know much about 'itanium', but i'd be surprised if they
chose 4-byte alignment for doubles.
oops. missed your reply.
http://h21007.www2.hp.com/dspp/tech/...01,180,00.html


fwiw, the same applies to HP-UX:
http://devresource.hp.com/drc/STK/do...64concepts.jsp

maybe this is a Windows issue?
http://msdn.microsoft.com/library/de..._bit_value.asp

I couldn't find anything on alignment, but maybe it's the LLP64 model
that's messing things up for the struct module? running the following
snippet with different values for TYPE might help us figure this out:

#define TYPE char // try short, int, long, float
typedef struct { TYPE c; double x; } st_double;
main() {
printf("align = %d\n", (sizeof(st_double) - sizeof(double)));
}

</F>

Dec 1 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Chandu | last post: by
3 posts views Thread by GrkEngineer | last post: by
6 posts views Thread by Daniel Rudy | last post: by
4 posts views Thread by Robert Kern | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.