1

I've been reading a lot of comments on the use of #pragma pack, nearly all negative. I understand what it does and I can see the negative effects it may have but is there an alternative? Most of the code I write is to read/write/manipulate GIS data. How can I read (say) a shapefile if I don't pack the struct? The same goes for most of the data files I read. I'm not talking about files I create but downloaded files. If I buy a shapefile set for a particular area/feature then it will follow the ESRI specifications and will have no padding. Therefore, to read the data, I have to make structs with byte alignment. I know how to do all this but I wondered if there was any other way? Shapefiles, in particular, have been around since the 90s and I am sure that they must have been used on many different architectures without causing segmentation faults etc. Obviously, I am careful to reset the pack at the end of the header file. As a secondary question, many years ago now, I was advised to put my structs that required packing into a single header file and put the #pragma pack at the beginning and #pragma pack (pop) right at the end as opposed to packing each individual struct. Is this the best way?

I have been successfully reading/writing byte aligned data files for years without problems but the clang compiler is now throwing up a warning: /xpfstructs.h:8: warning: Unterminated '#pragma pack (push, ...)' at end of file The last line before #endif is #pragma pack (pop)

Colins2
  • 11
  • 3
  • The pragma warning is a bug in clang, see https://stackoverflow.com/questions/72456118/why-does-clang-give-a-warning-unterminated-pragma-pack-push-at-end-of-f – Wintermute Nov 22 '22 at 11:45
  • 2
    If you have a specific binary file format with well-defined data and well-defined byte-order, using packed structures is usually considered okay. – Some programmer dude Nov 22 '22 at 11:45
  • 4
    I think `#pragam pack push/pop` is your best option. But just to offer an alternative, you can read the data as a array of bytes and manually move each field using `std::memcpy` to an unpacked structure. – Richard Critten Nov 22 '22 at 11:47
  • 2
    It's a bit of a matter of taste whether using `#pragma pack` or `__attribute__((packed))` is better, but to my mind it'd be strange to use anything other than packed data structures to handle files of this nature. It'd be possible to `memcpy` everything around, but it'd be slower and likely more error-prone. – Wintermute Nov 22 '22 at 11:50
  • 1
    `memcpy` of individual struct members rather than reading/writing entire (packed) `struct`s is the usual, portable alternative. – Jesper Juhl Nov 22 '22 at 12:02
  • Thanks for the advice. I will try the bytearray/memcpy option and see how well it works. On the thread mentioned by Wintermute, a contributor said that putting a single semicolon on a line just before the #pragma pack statement gets rid of the error. It works for me using Qt 6.4. I can't think why it should work but it does. – Colins2 Nov 23 '22 at 14:27

0 Answers0