开发者

Reading binary data into memory structures, weird effects

I've been at this for a while now and it really puzzles me. This is a very distilled code fragment that reproduces the problem:

uint8_t dataz[] = { 1, 2, 3, 4, 5, 6 };

struct mystruct {

    uint8_t dummy1[1];
    uint16_t very_important_data;
    uint8_t dummy2[3];

} *mystruct = (void *) dataz;

printf("%x\n", mystruct -> very_important_data);

What do you expect should be the output ? I'd say开发者_JS百科 x302, but nope. It gives me x403. The same as if using this structure:

struct mystruct {

    uint8_t dummy1[2];
    uint16_t very_important_data;
    uint8_t dummy2[2];

} *mystruct = (void *) dataz;

How would you explain that?


As others have mentioned, unless your compiler alignment is byte-aligned, your structure is likely to have "holes" in it. The compiler does this because it speeds up memory access.

If you're using gcc, there is a "packed" attribute which will cause the struct to be byte-aligned, and so remove the "holes":

struct __attribute((__packed__)) mystruct {
    uint8_t dummy1[1];
    uint16_t very_important_data;
    uint8_t dummy2[3];
} *mystruct = (void *) dataz;

However, this will not necessarily fix the problem. The 16-bit value may not be set to what you think it should be, depending on the endianness of your machine. You will have to swap the bytes in any multi-byte integers in the struct. There is no general function to do this, as it would require information on the layout of the structure at run-time, which C does not provide.

Mapping structures to binary data is generally non-portable, even if you get it to work on your machine, right now.


Packing. The is no guarantee how members of a struct are physically located inside the struct. They may be word-aligned, leaving gaps.

There are pragmas in some versions of C to explictly control packing.


Most likely, the compiler has added a byte of padding between dummy1 and very_important_data to align very_important_data on a 16-bit boundary.

In general, the alignment and padding of fields in a struct is implementation-dependent, so you shouldn't rely on it. If you absolutely need a particular behavior, many compilers offer #pragma or other directives to control this. Check your compiler's documentation.


It depends on the compiler, but usually a compiler aligns each member to its natural alignment. In the case you ran into, very_important_data is a uint16_t which probably has a natural alignment of 2 bytes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜