开发者

How to get register-width-independent unsigned integer variables in C?

开发者 https://www.devze.com 2023-01-31 22:07 出处:网络
I\'m writing code for Linux running on Intel x86 and x64 architectures (at least!) with gcc dumping and reading data structures from memory to disk as a cheap way to achieve serialization. Each field

I'm writing code for Linux running on Intel x86 and x64 architectures (at least!) with gcc dumping and reading data structures from memory to disk as a cheap way to achieve serialization. Each field should have no padding additionally.

To do so I've carefully organized the structures so that each field has just as many bits as they need to store information and hand-aligned them to 4 bytes boundaries (should that be eight?).

I started looking at the plethora of integer modifications (int, long int, long long int, unsigned long long int, etc.) but found no definitive source on the matter (and getting information about structures via dummy programs is boring and tedious and specific to my hardware.)

This said, I need som开发者_C百科e fields to be 16 bits, some fields to be 32 bits and some fields to be 64 bits. Is there a way to achieve this?


You can use the C99 header <stdint.h>, which defines fixed-width types like int16_t and uint32_t, among many others.

You will still need to be careful with padding and alignment, obviously, but if you know exactly how large each data member is, it shouldn't be too difficult. It would still be beneficial for you to take advantage of GCC's packed and aligned attributes to ensure correct alignment.

0

精彩评论

暂无评论...
验证码 换一张
取 消