gcc structures

Max S. king@greatfreeworld.org
Sun Sep 8 18:00:00 GMT 2013


How will you use this struct once you have it aligned the way you want?

If you use it as a tx/rx buffer, be aware that doubles aren't portable.
Let alone that ints could be different size on different machines.
Even if your just writing these structs to file. The resulting file will
not be portable across architectures.

//bar is packed; slower to access.
struct special_packed_machine_dependent_struct bar = ...;

//result is binary compatible only with similar machines.
write(fd, &bar, sizeof(bar));

If the task is small It may be better to fiddle the bits/bytes yourself.

uint8_t buff[HEADE_SIZE];

get_data_from_someplace(buff);

//bar is not aligned. therefore possibly faster to access.
struct special_machine_dependent_struct bar;

//construct MEMBER preserving byte order.
bar.member = (buff[U16_MEMBER_OFFSET+1]<<8)|(buff[U16_MEMBER_OFFSET]);

I don't know what GIS shapefiles are so... The above may be pure lunacy.

MS.



More information about the Gcc-help mailing list