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

A

ahn1

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 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
 
N

Neal Norwitz

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.

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
8

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

n
 
J

jepler

I'm guessing that the expected behavior is 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/tech_TechDocumentDetailPage_IDX/1,1701,180,00.html

Jeff

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

iD8DBQFDjvpLJd01MZaTXX0RAucFAJ0UKtJ4OksxIPVIELYGl5T8wXGYNACeKvIo
DvQaqZW/XMm3dq7lL/j9j5g=
=CusK
-----END PGP SIGNATURE-----
 
F

Fredrik Lundh

Neal said:
I wouldn't be surprised, but I don't understand the problem.


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
24

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

or isn't "native alignment" the default?

</F>
 
F

Fredrik Lundh

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.

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

maybe this is a Windows issue?
http://msdn.microsoft.com/library/d.../en-us/win64/win64/storing_a_64_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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top