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.

Pragma pack and code composer studio



Hello.

I have two projects: one in MS Visual studio and one in Code Composer. 

In MSVC project I have large header file with a lot of structures. All structures in this file are aligned by pragma pack:

#pragma pack(push,4)
typedef struct MyStruct
{
     int a;
     int b;
     int b;
}
MyStruct;

#pragma pack(pop)

But code composer does not support pragma pack.

I tried to redefine struct as below 

typedef struct MyStruct
{
    int a  __attribute__ ((aligned (4)));
    int b  __attribute__ ((aligned (4)));
    int b  __attribute__ ((aligned (4)));
}
MyStruct;

and enable gcc support, but it doesn't work.

How should I rewrite structures definitions?

Is there any way not to apply attributes to every pack or structure, but place lines is start and end of the file?

  • You cannot use attribute "aligned" to reduce the alignment of an object.  You must use #pragma pack or attribute "packed".

    The TI compiler lets you declare the struct type itself as "packed":

    struct MyStruct
    {
        int a;
        int b;
        int c;
    } __attribute__((packed));
    typedef struct MyStruct MyStruct;

    I don't think the TI compiler will let you declare regions of type definitions as packed; if so, I'm unaware of how to do it.

  • Hello.

    If I declare structure as packed does it equal #pragma pack (1)?
    How can I use pragma pack if it is not supported: e2e.ti.com/.../89148
    Or I have to enable some options?

  • Which target ISA are you compiling for (e.g. ARM, C6000)?
    What is the compiler version? (Note: the compiler version is not the same as the CCS version.)
  • Sitara Am335x, CortexA8, compiler is TI v. 5.2.5
  • The TI ARM compiler version 5.2.5 does support packing structures using the pragmas you first show.  Consider this example ...

    // Packed structure typedef 
    #pragma pack(push,1)
    typedef struct with_pack
    {
       short a;
       int b;
       int c;
    }
    with_pack;
    #pragma pack(pop)
    
    // No packing structure typedef
    typedef struct no_pack
    {
       short a;
       int b;
       int c;
    }
    no_pack;
    
    with_pack wp;  // packed
    no_pack   np;  // not packed

    Note how the packed structure is packed on 1 byte boundaries.  Packing on 4 byte boundaries, for a struct which contains nothing but int members, is the same as the compiler default.  Also note how the first member is short, not int.  If all the members are type int, there is no need to pack anything.

    When I build this code, I use the -k option to cause the compiler to keep the auto-generated assembly file.  When I inspect that file I see these lines ...

            .global wp
            .common wp,10,1
            .global np
            .common np,12,4

    Lines 1-2 define a packed structure named wp.  In the .common directive the 10 means the structure has a size of 10 bytes, and the 1 means it is aligned on a 1 byte boundary.  Lines 3-4 defined a normal (not packed) structure named np.  It has a size of 12 bytes, because there is a 2 byte gap between the short a and the int b.  Note how np is aligned on a 4-byte boundary.

    Thanks and regards,

    -George

  • Did you compile the example with some additional options?
    I tried but this example didn't worked .

    Where can I read about #pragma pack and TI compilers?

  • Ilya Sokolov1 said:
    Did you compile the example with some additional options?

    No.

    Ilya Sokolov1 said:
    I tried but this example didn't worked .

    What do you see that makes you think it didn't work?  Exactly how do you see it?

    Ilya Sokolov1 said:
    Where can I read about #pragma pack and TI compilers?

    Please see the ARM compiler manual.

    Thanks and regards,

    -George

  • Hello.

    I defined structure in Code Composer as listen below:

    #pragma pack(push,4)
    typedef struct SYSTEMTIME357 {
    WORD wYear ;
    WORD wMonth;
    WORD wDayOfWeek;
    WORD wDay;
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
    } SYSTEMTIME357 ;

    #pragma pack(pop)

    By the same code I defined this structure in MS Visual Studio.
    Then I initialized structure and tried d to send this structure over TCP from Sitara to PC. But I got some rubbish, but not data I want to send.
    Then I tried to replace WORD by int, transmission worked.
    Now I'm using __attribute__((packed)) in Code Composer and #pragma pack(1) in Visual Studio and it works.

  • I presume WORD is the same type as int.  In that case this ...

    Ilya Sokolov1 said:
    #pragma pack(push,4)

    has no effect.  That matches the compiler default of the TI ARM compiler. 

    Ilya Sokolov1 said:
    Then I initialized structure and tried d to send this structure over TCP from Sitara to PC. But I got some rubbish, but not data I want to send.

    I presume the layout of the structure has to match for this to work.  Rather than relying on a custom typedef like WORD, I suggest you use int32_t from the standard header file <stdint.h>.  The compiler, whether TI or Microsoft, guarantees this is a 32-bit wide signed type.  If you need an unsigned type, use uint32_t. 

    Thanks and regards,

    -George

  • Thank you for your answer.
    I have recreated project and #pragma pack worked.
    Maybe it was something wrong with project settings.
  • If your data structure is used for "data packet" purpose, then you should define data member with precise length. For this, you should not use "WORD" or "int". What is the length of a WORD variable? My suggestion is to include <stdint.h> and then use uint32_t for "WORD" and int16_t for "short".... etc.

    You may think "int" is safe. Well, what is the length of a "int" variable in 64-bit compiler? It is not always 4 bytes. For examples, it is 2 bytes in MSP430.