• Join
  • Sign In with my.TI Login
Texas Instruments
  • Products
  • Applications
  • Tools & Software
  • Support & Community
  • Sample & Buy
  • About TI
Sample & Purchase Cart Sample & Purchase Cart
  • Search
  • Advanced
TI E2E™ Community
  • Support Forums
  • Blogs
  • Groups
  • Videos
  • 简体中文
  • More ...
TI Home » TI E2E Community » Support Forums » Development Tools » TI C/C++ Compiler » TI C/C++ Compiler - Forum » unexpected behaviour of sizeof()/hole in a structure size
Share
TI C/C++ Compiler
  • Forum
Options
  • Subscribe via RSS

Forums

unexpected behaviour of sizeof()/hole in a structure size

This question is answered
Vishnu Beema
Posted by Vishnu Beema
on Mar 20 2012 09:49 AM
Intellectual445 points

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;

  1. As shown in above example, because of padding/structure alignment, the size of structure A will be 8.
  2. In structure B to avoid extra space, i rearranged the structure members. With this the members are allocated sequentially without any padding. So the size should be 7. But to my strange, the size of structure B is still 8. Extra variable is added as part of the structures. Please look into the attached document for more details.
  3. To cross verify i created one more Structure C. All the members are allocated sequentially and the size of the structure is as expected and it is 5.

My Query is:

  1. How come structure B is 8 instead of 7.
  2. If structure B is 8 then structure C should also be 6, but it is as expected and it is 5.

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

code Composer studio C 28335 C2000 Compiler
Report Abuse
  • Reply
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
All Replies
  • Archaeologist
    Posted by Archaeologist
    on Mar 20 2012 10:08 AM
    Suggested Answer
    Mastermind41365 points

    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.

    FAQ
    Report Abuse
    • Reply
    You have posted to a forum that requires a moderator to approve posts before they are publicly available.
  • Vishnu Beema
    Posted by Vishnu Beema
    on Mar 21 2012 01:44 AM
    Verified Answer
    Verified by Vishnu Beema
    Intellectual445 points

    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.

    Thanks & Regards

    Vishnu Beema

    Report Abuse
    • Reply
    You have posted to a forum that requires a moderator to approve posts before they are publicly available.
  • Douglas Gwyn
    Posted by Douglas Gwyn
    on Jun 01 2012 19:44 PM
    Expert2060 points

    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.

     

    Report Abuse
    • Reply
    You have posted to a forum that requires a moderator to approve posts before they are publicly available.
  • Vishnu Beema
    Posted by Vishnu Beema
    on Oct 15 2012 07:30 AM
    Intellectual445 points

    Hi,

    I have small query.

    In TI controller, can't i use any #pragram (pack) and remove padding.

    Thanks & Regards

    Vishnu Beema

     

    Report Abuse
    • Reply
    You have posted to a forum that requires a moderator to approve posts before they are publicly available.
TI E2E™ Community
  • Support Forums
  • Blogs
  • Videos
  • Groups
  • Site Support & Feedback
  • Settings
TI E2E™ Community Groups
  • TI University Program
  • Make the Switch
  • Microcontroller Projects
  • Motor Drive & Control
Other Communities
  • Deyisupport
  • Designsomething.org
  • beagleboard.org
  • TI on Element 14
  • TI on TechXchangeSM
Other Technical & Support Resources
  • WEBENCH® Design Center
  • Product Information Centers
  • Technical Documents
  • TI Design Network
  • TI Technical Articles
  • TI Training

All content and materials on this site are provided "as is". TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with regard to these materials, including but not limited to all implied warranties and conditions of merchantability, fitness for a particular purpose, title and non-infringement of any third party intellectual property right. TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with respect to these materials. No license, either express or implied, by estoppel or otherwise, is granted by TI. Use of the information on this site may require a license from a third party, or a license from TI.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms of Use of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms of Use of this site. TI, its suppliers and providers of content reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Follow Us Texas Instruments on Facebook Texas Instruments on Twitter Texas Instruments on LinkedIn Texas Instruments on Google+
TI Worldwide | Contact Us | my.TI Login | Site Map | Corporate Citizenship | mobile m.ti.com (Mobile Version)

TI is a global semiconductor design and manufacturing company. Innovate with 100,000+ analog ICs and
embedded processors, along with software, tools and the industry’s largest sales/support staff.

© Copyright 1995-2013 Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy Policy | Terms of Use