- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个用 C 语言编写的打包结构,我想用 Python 对其进行解析。我注意到 C(使用 GCC 4.9.2)和 Python 3.4.2 中的 ctypes 库之间的运算符 sizeof
返回的位域结构大小存在差异。
以下 C 代码按预期打印 5:
#include <stdio.h>
#include <stdint.h>
typedef struct __attribute__((packed)) {
uint32_t ch0 : 20;
uint32_t ch1 : 20;
} pkt_t;
int main(){
printf("sizeof(pkt_t): %d\n", sizeof(pkt_t));
return 0;
}
虽然 Python 中的(相同)代码打印出 8
import ctypes
class Packet(ctypes.LittleEndianStructure):
_pack_ = 1
_fields_ = [
('ch0', ctypes.c_uint32, 20),
('ch1', ctypes.c_uint32, 20),
]
print(ctypes.sizeof(Packet()))
看起来 _pack_ = 1
等同于 C 中的 __attribute__((aligned(1)))
,而不是 __attribute__((packed, aligned (1)))
这将使结构尽可能紧密地打包。有没有办法为 ctypes 结构启用 packed
属性?
最佳答案
ctypes 文档 https://docs.python.org/3/library/ctypes.html#structure-union-alignment-and-byte-order明确指出
By default, Structure and Union fields are aligned in the same way theC compiler does it. It is possible to override this behavior byspecifying a pack class attribute in the subclass definition. Thismust be set to a positive integer and specifies the maximum alignmentfor the fields. This is what #pragma pack(n) also does in MSVC.
这意味着它们不引用 gcc 的作用,而是引用 MSVC #pragma pack
。请参阅:https://learn.microsoft.com/en-us/cpp/preprocessor/pack?view=vs-2019
n
(Optional) Specifies the value, in bytes, to be used for packing. Ifthe compiler option /Zp isn't set for the module, the default valuefor n is 8. Valid values are 1, 2, 4, 8, and 16. The alignment of amember is on a boundary that's either a multiple of n, or a multipleof the size of the member, whichever is smaller.
因此,观察到的行为的原因之一在于 MSVC 与 gcc 的编译器细节。
gcc 文档 https://gcc.gnu.org/onlinedocs/gcc-4.9.4/gcc/Type-Attributes.html#Type-Attributes (抱歉,gcc 页面上没有 gcc-4.9.2 的文档),另一方面,cleary 告诉我们:
This attribute(packed), attached to struct or union type definition, specifiesthat each member (other than zero-width bit-fields) of the structureor union is placed to minimize the memory required. When attached toan enum definition, it indicates that the smallest integral typeshould be used.
在这种情况下,它们并没有说明任何边界要求,而内存占用量已最小化。跨字节边界放置位域是否有意义可能是另一个讨论的主题。为什么 __attribute__((aligned(1)))
实际上按预期设置结构,这对我来说似乎很合乎逻辑且一致,因为它明确地强制执行结构成员的字节对齐,这似乎不是通过指定 __attribute__((packed))
来区分大小写。在第一种情况下,gcc 编译器也会对位字段强制执行字节对齐。
总结:
__attribute__ ((aligned (n)))
要求结构(成员)的对齐__attribute__ ((__packed__))
要求最小化内存占用,即使这意味着位域字节边界被跨越关于c - ctypes.struct(打包)中的 sizeof 与 C 中的打包结构不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40454606/
这个问题已经有答案了: Why isn't sizeof for a struct equal to the sum of sizeof of each member? (13 个回答) 已关闭 8
先生。 Stroustrup 在他的新书(TCPL 第 4 版)第 149 页写下了以下内容 1 N && sizeof(long)<=N任何 N 值的实现,更不用说任何人都会考虑使用 wchar_t
如 [5.3.3/3] 所述(expr.sizeof,工作草案): The sizeof operator can be applied to a pointer to a function, but
从C标准来看,int至少有16bit,long至少有32bit,long long如果有的话至少有64bit(有些平台可能不支持)。只是想知道标题中的句子是否总是正确的。 最佳答案 没有。该标准仅定义
我运行的是 Windows 7(64 位)。 这个问题与此处找到的问题相同: long on a 64 bit machine 但更深入,因为它处理更多的数据类型并适用到 C 或 C++,而不是 C#
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 8年前关闭。 Improve this
这个问题在这里已经有了答案: Length of array in function argument (9 个回答) 关闭 9 年前。 #include void printS(char []);
我承认这三个都有不同的含义。但是,我不明白这些具体情况适用于哪些特定情况。任何人都可以分享每个例子吗?谢谢。 malloc(sizeof(int)) malloc(size
To avoid things quietly breaking if you change the array size, I suggest std::copy(a, a + sizeof(a)/
我在 python 中注意到以下事实: >>> (1, 2, 3).__sizeof__() 48 >>> [1, 2, 3].__sizeof__() 64 我理解列表和元组之间的区别,但我希望它们
是否存在与指针大小相同的整数类型?保证所有微架构? 最佳答案 根据 this Wikipedia page ,在 C99 中,您的 stdint.h header 可能声明了 intptr_t 和 u
我注意到 int 和 double 的大小与使用函数 MPI_Type_size(MPI_INT, &MPI_INT_SIZE); 计算的不同。这是否意味着 sizeof(MPI_INT) 返回了错误
这个问题已经有答案了: How to find the size of an array (from a pointer pointing to the first element array)? (
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
我是一名优秀的程序员,十分优秀!