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.

Help with struct aligned in dm642



Hi all!

   I want to send a struct data to the pc throw internet,

for example

typedef struct _rect{

BYTE x;

short y;

int width;

BYTE height;

}RECT; 

when i use the send function,usually i will use in the follow ways:

RECT rdata;

send(&rdata,sizeof(RECT )),

however, the sizeof(RECT ) is not the real size i want to send,because the BYTE alignment,

in vc6.0, this can be solved in the follow way:

#pragma pack(1)

typedef struct _rect{

BYTE x;

short y;

int width;

BYTE height;

}RECT; 

#pragma pack()

but  in dm642 ,

this result in "warning: unrecognized #pragma",

so how to do "#pragma pack(1)" in dm642,or is there any better ways can be used ?

 i also see the #pragma STRUCT_ALIGN (type, constant expression);

but it does not work. 

Thanks in advance. Your help will be greatly appreciated!

  • The TI compilers do not support packed structures. 
    The members of a "struct" are laid out in order.  All members are aligned to the appropriate alignment of its type.  If necessary, padding is added before members to reach the correct alignment. Padding is also added to the end of the struct to round it out to a multiple of the strictest alignment of any of its members.  The struct also inherits the alignment requirement of that member.  The TI compilers do not support a "packed" struct type.

    One thing that you could try is to reorder the elements of the structure. For example:

    typedef struct _rect{

    int width;

    short y;

    BYTE height;

    BYTE x;

    }RECT; 

  • Hi,Mariana!

     thank you for your help! 

    as you say, The TI compilers do not support packed structures,

    that means i have copy all members to another buffer,

    this is not so convenient, is there any other way to do it,

    in condition i don't want to reorder the elements of the structure?

     

  • Hi Xavier,

    I do not know of another way of doing it..

     

  • Your best solution will depend on what is most important to you. You clearly understand that your preferred way to define your struct is not purely portable since even the size of short and int are not defined to be identical from one implementation to another.

    With that understanding, your choices are how you want to use the various members of your ideally packed struct and how you want to use the struct as a whole for transmission over ethernet.

    You can write the struct as an array of 8 bytes, and that would allow you to use it easily for the transmission command, send(). This will also allow the storage space to be minimized.

    typedef char RECT[8];
    RECT rdata;
    send(&rdata,sizeof(RECT));

    You can then write macros or functions to get and set the four elements using intrinsics.

    #define GET_x(rd) ((BYTE)((rd)[0]))
    #define GET_y(rd) ((short)_extu(*((int*)(rd)),8,16)&0xff)
    #define GET_width(rd) (_mem4((void*)((char *)(rd)+3)))
    #define GET_height(rd) ((BYTE)((rd)[7]))

    Similar macros could be written for SET_xyz. [I have not tried to compile these examples, so there may be paren mismatches and other syntax errors, but the idea is there for you.]

    But this may require some changes to your code.

    Your code would probably be changed the least by doing what you already suggested, which is to transfer between the struct you want and a buffer for the the transmission.

  • Hi all:

      Thank you for your help again!

    [:Y] 

  • > You clearly understand that your preferred way to define your struct is not purely portable since even the size of short and int are not defined to be identical from one implementation to another.

    That argument is specious. Ever since C99, we have been able to specify precise types via the stdint.h library, e.g., uint16_t, int32_t, etc.

    --Randy

    I am incredulous that the 64x compiler doesn't support structure packing. It was supported on the C55x compiler, and has been supported in the GCC realm for decades. It just cost me about 2 days of work.

     

  • I am glad you solved your issue with struct packing on the C64x. And we agree it is difficult to have to switch between the different-featured tools within the TI DSP family.

    If you want answers or discussion on the compiler features or rationale, the TI C/C++ Compiler - Forum is the best place to post a new thread for that discussion. Only the four of us already on this thread will get notice of your concerns, rather than the compiler developers who may be monitoring the compiler forum.