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.

Compiler/TMS320C6654: Printf issue with pointer on unaligned data

Part Number: TMS320C6654

Tool/software: TI C/C++ Compiler

Hi,

I have an issue with printf on TMS320C6654 with compiler 8.3.5 when I want to print a data which is not aligned.

Following code reproduce this issue (I use packed attribute to create unaligned data):

typedef struct{
    uint8_t idx;
    float array[10];
} T_UNPACKED;

typedef struct{
    uint8_t idx;
    float array[10];
} __attribute__((packed)) T_PACKED;

int main(void)
{
    T_UNPACKED data_unpacked;
    T_PACKED data_packed;

    data_unpacked.array[0] = 51;
    data_packed.array[0] = data_unpacked.array[0];

    printf("%d:%g - %d:%g\n", data_unpacked.array, data_unpacked.array[0], data_packed.array, data_packed.array[0]);

    float* ptr_unpacked = data_unpacked.array;
    float* ptr_packed = data_packed.array;
    printf("%d:%g - %d:%g\n", ptr_unpacked, ptr_unpacked[0], ptr_packed, ptr_packed[0]);
	
    return;
}

this give me :

9349380:51 - 9349461:51
9349380:51 - 9349461:3.35544e+07

So the printf of the unaligned data accessed by pointer is false (like if pointer have been realigned). My example is with float, but it seems to be the same with other types

Is it a known behaviour? How to avoid it?

Thanks

  • Hello!

    In compiler documentation

    6.15.4. Type Attributes
    6.15. GNU Language Extensions

    there is a statement:

    It is illegal to implicitly or explicitly cast the address of a packed struct member as a pointer to any non-packed type except an unsigned char.

    Perhaps this is your case.

  • Hi,

    This explain what I see.

    Just a question: if it is illegal, why there is no error message at compilation instead of having undefined behaviour at execution?

    regards

  • Regarding the question about why doesn't the compiler issue a diagnostic.  I can understand why you might think it would be easy to detect this case ...

    Gildas ROLLAND said:
    float* ptr_packed = data_packed.array;

    But that is not typical usage.  This case is more common ...

        function_call(data_packed.array);
    
        /* in a different file */
        void function_call(float *ptr_to_float)
        {
           ...
        }

    In that case, there is no way to detect the problem.  Because such common cases cannot be detected, no time was spent on detecting any of them.

    Thanks and regards,

    -George

  • Hello,

    Honestly, when I read C6000 compiler documentation on packed structures for the last time, it was simply "We don't do it here". I wanted to reference that clause, but was surprised to see packing is now supported as GNU extension.

    Perhaps packing was asked to overlay structures over byte stream. As it was already explained, all possible use cases are hard to account. In our group we  live with no packing on DSP for so long, that no one even complains, just this is the way.