packed data strcutures
-
I'm looking for the best C++ solution to this data situation. I need something as generic C++ as I can get because we may switch compilers sometime in the near future. I'm implimenting an ISO9660 filesystem, which stores on disk structures that look something like this in C #pragma pack(1) struct filename { BYTE version; ULONG bigendianBlockAddress; ULONG littleendianBlockAddress; BYTE filenameLenght; BYTE filename[30]; // not C style string #pragma pack() That is block address that are both little endian and big endian, but otherwise exactly the same value. The perfect situation for a class. something like: #pragma pack(1) class bothorder { ULONG big; ULONG little; public: ULONG operator=(...) ... }; This structur occures all over, and manipulating it will be a large part of the rest of my code. Now I get (through an interface that I cannot change) a pointer memory space with the first structure. Is it safe to use assignment new or some cast to place the class exactly there, or should I expect compilers to use more than the 8 bytes the ULONG takes for some internal use? (such as a this pointer?) Is there a better way to solve this problem? I'd like to make the code as readable as I can. I know pragma pack isn't in standard C++, but it appears all compilers support it. Those who know ISO9660 will note a big advantage for classes: I intentionally introduced a bug, the little endian number should have been before the big endian number!
-
I'm looking for the best C++ solution to this data situation. I need something as generic C++ as I can get because we may switch compilers sometime in the near future. I'm implimenting an ISO9660 filesystem, which stores on disk structures that look something like this in C #pragma pack(1) struct filename { BYTE version; ULONG bigendianBlockAddress; ULONG littleendianBlockAddress; BYTE filenameLenght; BYTE filename[30]; // not C style string #pragma pack() That is block address that are both little endian and big endian, but otherwise exactly the same value. The perfect situation for a class. something like: #pragma pack(1) class bothorder { ULONG big; ULONG little; public: ULONG operator=(...) ... }; This structur occures all over, and manipulating it will be a large part of the rest of my code. Now I get (through an interface that I cannot change) a pointer memory space with the first structure. Is it safe to use assignment new or some cast to place the class exactly there, or should I expect compilers to use more than the 8 bytes the ULONG takes for some internal use? (such as a this pointer?) Is there a better way to solve this problem? I'd like to make the code as readable as I can. I know pragma pack isn't in standard C++, but it appears all compilers support it. Those who know ISO9660 will note a big advantage for classes: I intentionally introduced a bug, the little endian number should have been before the big endian number!
-
When you use #pragma pack(1) you can be sure your data will be aligned on one byte boundary, so will be in size exactly as you defined it. Although it works for unions and structures only.
thank you, thats what I needed to know. My compiler also allows this for class, but I don't expect to use this one forever. (It isn't supported anymore, and is missing some parts of C++) I makes for ugly code (though I see why not all compilers will make it work for class) Anyone have a better idea than a bunch of structs, and some classes to manipulate them with pointers to the struct?