This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

cl430 compiler problem



Hi all. I am a problem with cl430 compiler.

1. I am my structure, it real size is 17 bytes. If i make sizeof(mystruct) it returns 18 bytes. Why?

2. StructPtr *pStruct=(pStruct *)&buf[(SIZEOF_BUF-1)]; - first byte is broken;

StructPtr *pStruct=(pStruct *)&buf[(SIZEOF_BUF-2)]; - first byte is NOT broken;

Why odd pointer doesn't work normal?

  • 1. Structures have trailing padding for alignment for when the structures are in an array.  Your structure has at least one member that is 2-byte aligned.

    2.  What do you mean by "broken"?  Can you provide a test case that demonstrates the problem?

  • StructPtr *pStruct=(pStruct *)&buf[(SIZEOF_BUF-1)];

    ReadFromFile(fName, pStruct);

    n = pStruct->FirstMember;

    ///

    in n is not correct data.

     

    StructPtr *pStruct=(pStruct *)&buf[(SIZEOF_BUF-2)];

    ReadFromFile(fName, pStruct);

    n = pStruct->FirstMember;

      ///

    in n is correct data

     

    StructPtr Struct;

    StructPtr* pStruct=Struct;

    ReadFromFile(fName, pStruct);

    n = pStruct->FirstMember;

      ///

    in n is correct data

  • I assume buf is an array of unsigned char.  It is not valid to cast an odd address to a "StructPtr *".  Standard C does not allow casting an arbitrary unsigned char pointer to a pointer  with stricter alignment.

    Are you attempting to treat StructPtr as a packed structure?

  • thx for your answers.

    Yes, Struct is a packed structure

    Archaeologist said:
    Standard C does not allow casting an arbitrary unsigned char pointer to a pointer  with stricter alignment.

    .

    why? It is not a big problem for me, but i want to undestand this behavior

    This code worked on older project on x51 core processor.

     

  • Alexander Anusat said:

    Yes, Struct is a packed structure

    Is it actually declared with the packed attribute?

    Alexander Anusat said:

    Standard C does not allow casting an arbitrary unsigned char pointer to a pointer  with stricter alignment.

    .

    why? It is not a big problem for me, but i want to undestand this behavior

    [/quote]

    It's not legal because it's unlikely to work on most hardware.  See http://c-faq.com/strangeprob/ptralign.html

    Alexander Anusat said:

    This code worked on older project on x51 core processor.

    Depending on the properties of the processor and compiler, casting to a more strictly-aligned pointer can work sometimes, but it's not a guarantee that it will always work.