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. Mobile Development
  3. Mobile
  4. Datatype misalignment

Datatype misalignment

Scheduled Pinned Locked Moved Mobile
helpperformance
3 Posts 2 Posters 3 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.
  • C Offline
    C Offline
    Cedric Moonen
    wrote on last edited by
    #1

    Hello! I have a structure that contains several informations. This structure can be read directly from a file. In this file I have a "memory copy" of the structure, so I just have to make a fread (with the adress of the structure as first parameter) to import the complete structure. For compatibility, I NEED (!!) to have this structure with a packing alignment of 1 byte:

    #pragma pack(1)
    struct SOMESTRUCT
    {
    .... //Some fields here
    char data[]; //Buffer that will hold data in a single block
    }
    #pragma pack() //restore to original byte alignment setting

    This works fine when I read the structure in the file. The problem occurs when I try to "put" data in the data field:

    SOMESTRUCT* temp = .....; //Initialized here from another function, works fine
    int SomeValue = 3;
    *((int*)temp->data)=SomeValue; //Exception thrown here: datatype misalignment !

    This works fine on the emulator but not on the remote device !! So, I really don't know what to do!! Thanks for help :) !

    J 1 Reply Last reply
    0
    • C Cedric Moonen

      Hello! I have a structure that contains several informations. This structure can be read directly from a file. In this file I have a "memory copy" of the structure, so I just have to make a fread (with the adress of the structure as first parameter) to import the complete structure. For compatibility, I NEED (!!) to have this structure with a packing alignment of 1 byte:

      #pragma pack(1)
      struct SOMESTRUCT
      {
      .... //Some fields here
      char data[]; //Buffer that will hold data in a single block
      }
      #pragma pack() //restore to original byte alignment setting

      This works fine when I read the structure in the file. The problem occurs when I try to "put" data in the data field:

      SOMESTRUCT* temp = .....; //Initialized here from another function, works fine
      int SomeValue = 3;
      *((int*)temp->data)=SomeValue; //Exception thrown here: datatype misalignment !

      This works fine on the emulator but not on the remote device !! So, I really don't know what to do!! Thanks for help :) !

      J Offline
      J Offline
      Jonas Larsson
      wrote on last edited by
      #2

      cedric moonen wrote: This works fine on the emulator but not on the remote device !! If I remember correctly, datatypes like int needs to be placed on even (or was it divisble by 4) memory addresses on Windows CE. Its unclear to me if your char data[] will be placed at an illegal address for an int, but that might be it. A workaround would be using byte-wise copy, or perhaps re-order the struct. Jonas “Our solar system is Jupiter and a bunch of junk” - Charley Lineweaver 2002

      C 1 Reply Last reply
      0
      • J Jonas Larsson

        cedric moonen wrote: This works fine on the emulator but not on the remote device !! If I remember correctly, datatypes like int needs to be placed on even (or was it divisble by 4) memory addresses on Windows CE. Its unclear to me if your char data[] will be placed at an illegal address for an int, but that might be it. A workaround would be using byte-wise copy, or perhaps re-order the struct. Jonas “Our solar system is Jupiter and a bunch of junk” - Charley Lineweaver 2002

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        Hi, thanks for suggestion. I replaced *((int*)temp->data)=SomeValue; by a memcpy and the bug disapeared !! Thanks a lot :) :)

        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