Hi,
I am using TMS320F28335 controller and i am compiling using code composer. I am tyring to use sizeof() in my code, and even after removing structure padding i am finding an unexpected behaviour of sizeof() operator.
I defined 3 structures as shown below.
Structure A:
typedef struct A_Tag{ // Var No Add Loc //---------------------------- int uVar1; //1: 0 int uVar2; //2: 1 int uVar3; //3: 2 int uVar4; //4: 3 int uVar5; //5: 4 (because of structure padding float will not be allocated at 5) float floatVar6; //6: 6-7}A_Struct;
Structure B:
typedef struct B_Tag{ // Var No Add Loc //---------------------------- float floatVar1; // 1: 0 - 1 int uVar2; // 2: 2 int uVar3; // 3: 3 int uVar4; // 4: 4 int uVar5; // 5: 5 int uVar6; // 6: 6 (Size should be 7, but its showing as 8)}B_Struct;
Structure C:
typedef struct C_Tag{ // Var No Add Loc //---------------------------- int uVar1; // 1: 0 int uVar2; // 2: 1 int uVar3; // 3: 2 int uVar4; // 4: 3 int uVar5; // 5: 4 (size of the structure is 5)}C_Struct;
My Query is:
For more details please see attached document and map file.
Detail snapshots:
7484.sizeof()_issues.doc
Map file
4265.rtl_test.txt
Thanks & Regards
Vishnu Beema
The alignment of a structure is the maximum of the alignment of each member.
The size of a structure is rounded up to a multiple of the alignment of the structure.
Structure B is aligned to 32 bits, therefore it is rounded up.
Structure C is aligned to 16 bits, therefore no rounding up is needed.
Hello,
Till now i used to see most of the blogs with respect to padding in between structure members. Now i came to know even there will be padding at end of structure members, to achieve 32-bit alignment.
Better to capture in wiki sites of TI with examples.
Once again thank you for your comments.
Padding at the end of a structure is necessary to ensure proper alignment (of all of its members) when the structure is used as an element of an array. In order to avoid complexity, the compiler doesn't make an exception when it doesn't see any array of the structure.
I have small query.
In TI controller, can't i use any #pragram (pack) and remove padding.