2024-10-08 19:19:03 发布
网友
file1 = open("test.txt", 'wb') file1.write(struct.pack('icic', 1, '\t', 2, '\n')) file1.close() print os.path.getsize("test.txt")
它给我13分。但我认为应该是4+1+4+1=10字节。似乎它存储了一个字节的'\n',但是存储了4个字节的'\t'。你有什么想法? 谢谢!你知道吗
要获得实际的结构大小,请使用^{}:
>>> import struct >>> struct.calcsize('icic') 13
这是因为您使用的是默认对齐方式,然后应用C规则:
By default, C types 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).
第一个ic将只有5个字节,但是如果您列出它两次,C将把它填充到8,因此下一个ic对将它带到13。如果您使用3 ic对,您将得到21对,以此类推。C填充i整数以对齐到4字节组。此data structure alignment用于提高内存性能,但在尝试将其用于不同目的时可能会出现意外情况。你知道吗
ic
i
选择显式字节顺序:
>>> struct.calcsize('>icic') 10
参见Byte Order, Size and Alignment section。你知道吗
要获得实际的结构大小,请使用^{} :
这是因为您使用的是默认对齐方式,然后应用C规则:
第一个
ic
将只有5个字节,但是如果您列出它两次,C将把它填充到8,因此下一个ic
对将它带到13。如果您使用3ic
对,您将得到21对,以此类推。C填充i
整数以对齐到4字节组。此data structure alignment用于提高内存性能,但在尝试将其用于不同目的时可能会出现意外情况。你知道吗选择显式字节顺序:
参见Byte Order, Size and Alignment section。你知道吗
相关问题 更多 >
编程相关推荐