none
Datatype misalignment error

    Question

  • Hi,
    I'm actually de developing an application under windows mobile. I have know a problem sometimes I have an error : "
    Unhandled exception at 0x0001cff4 in ... : 0x80000002: Datatype misalignment."
     I don't understand what is the problem and what mean this error. The last modifications I made on my program was absolutly not linked to the the part of the software that cause now this error.
    Here is the complete error message :

    Data Abort: Thread=8e719450 Proc=8c3a5ef0 'mobileSender.exe'
    AKY=00020001 PC=0001cff4(mobileSender.exe+0x0000cff4) RA=000198ec(mobileSender.exe+0x000098ec) BVA=24073ff6 FSR=00000001
    Unhandled exception at 0x0001cff4 in mobileSender.exe: 0x80000002: Datatype misalignment.

    And here is the part of the code wich is problematic.

    ... 
     
    typedef struct _Header { 
        char source[6]; 
        int seqNum; 
        short age; 
        short payloadLength; 
    } Header; 
     
    ... 
     void Packet::build(Message *message, char *buffer, int *plength)
    Header *header = (Header *)buffer; 
     
    ... 
     
    header->seqNum = (int) message->msgKey.seqnum; 
    header->age = (short) message->getAge(); 
     
    ... 
     

    Friday, December 26, 2008 3:36 PM

Answers

  • It is interesting to look at stack trace with provided parameters (especially buffer). Is buffer valid parameter passed to the method by caller or was data on stack corrupted?
    It is not clear where error occurs. I would expect it at line
    header->seqNum = (int) message->msgKey.seqnum;
    AFAIR BVA=24073ff6 is address of failed data and 0x24073ff6 is pointer to address that is not divisible by 4. On ARM devices 4-byte int variables should be 4-byte alligned. And if you have non-4-bytes alligned buffer pointer you'll have such exception. Consider such code:
    char* buf = new char[20]; 
    Header* h0 = (Header*) buf; 
    Header* h4 = (Header*) (buf+4); 
    Header* h2 = (Header*) (buf+2); 
     
    h0->seqNum = 1; 
    h4->seqNum = 4; 
    h2->seqNum = 2; 
     

    Bold line will fail with similar exception because seqNum field in structure referenced by h2 is misaligned.
    • Edited by Serg2008 Friday, December 26, 2008 8:31 PM typos
    • Marked as answer by warrentangModerator Thursday, January 22, 2009 6:56 AM
    Friday, December 26, 2008 8:30 PM

All replies

  • It is interesting to look at stack trace with provided parameters (especially buffer). Is buffer valid parameter passed to the method by caller or was data on stack corrupted?
    It is not clear where error occurs. I would expect it at line
    header->seqNum = (int) message->msgKey.seqnum;
    AFAIR BVA=24073ff6 is address of failed data and 0x24073ff6 is pointer to address that is not divisible by 4. On ARM devices 4-byte int variables should be 4-byte alligned. And if you have non-4-bytes alligned buffer pointer you'll have such exception. Consider such code:
    char* buf = new char[20]; 
    Header* h0 = (Header*) buf; 
    Header* h4 = (Header*) (buf+4); 
    Header* h2 = (Header*) (buf+2); 
     
    h0->seqNum = 1; 
    h4->seqNum = 4; 
    h2->seqNum = 2; 
     

    Bold line will fail with similar exception because seqNum field in structure referenced by h2 is misaligned.
    • Edited by Serg2008 Friday, December 26, 2008 8:31 PM typos
    • Marked as answer by warrentangModerator Thursday, January 22, 2009 6:56 AM
    Friday, December 26, 2008 8:30 PM
  • I have seen this error message several times after importing/loading a embedded Visual C project into Visual Studio 2003/2005/2008.

    The solution for me was to start a new SmartDevice C/C++ project and importing all the code of the EVC project and then it runs fine.


    hjg

    Wednesday, May 14, 2014 8:12 AM