Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. packed data strcutures

packed data strcutures

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++jsonperformancequestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Henry miller
    wrote on last edited by
    #1

    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!

    V 1 Reply Last reply
    0
    • H Henry miller

      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!

      V Offline
      V Offline
      vmaltsev
      wrote on last edited by
      #2

      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.

      H 1 Reply Last reply
      0
      • V vmaltsev

        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.

        H Offline
        H Offline
        Henry miller
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups