Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 215809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:30:20+00:00 2026-05-11T18:30:20+00:00

I’ve spent a few minutes manually re-ordering fields in a struct in order to

  • 0

I’ve spent a few minutes manually re-ordering fields in a struct in order to reduce padding effects[1], which feels like a few minutes too much. My gut feeling says that my time could probably be better spent writing up a Perl script or whatnot to do this kind of optimization for me.

My question is whether this too is redundant; is there already some tool that I’m not aware of, or some compiler feature that I should be able to turn on[2] to pack structs?

The issue is even more complicated by the fact that this needs to be consistently optimized across a few different architectures, so whatever tool used needs to be able to account for different struct alignments and pointer sizes as well.

EDIT: A quick clarification — what I want to do is re-order the field in the source code in order to avoid padding, not “pack” the struct as is compiling without padding.

EDIT #2: Another complication: depending on the configuration, sizes of some data types may also change. The obvious ones are pointers and pointer-diffs for different architectures, but also floating-point types (16, 32 or 64-bit depending on the ‘exactness’), checksums (8 or 16-bit depending on ‘speed’) and some other non-obvious stuff.

[1] The struct in question is instantiated thousands of times on an embedded device, so each 4-byte reduction of the struct could mean the difference between a go and no-go for this project.

[2] Available compilers are GCC 3.* and 4.* , Visual Studio, TCC, ARM ADS 1.2, RVCT 3.* and a few others more obscure.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:30:20+00:00Added an answer on May 11, 2026 at 6:30 pm

    If every single word you can squeeze out of the storage is critical, then I have to recommend optimizing the struct by hand. A tool could arrange the members optimally for you, but it doesn’t know, for example, that this value here that you’re storing in 16 bits actually never goes above 1024, so you could steal the upper 6 bits for this value over here…

    So a human will almost certainly beat a robot on this job.

    [Edit] But it seems like you really don’t want to hand-optimize your structs for each architecture. Maybe you really have a great many architectures to support?

    I do think this problem isn’t amenable to a general solution, but you might be able to encode your domain knowledge into a custom Perl/Python/something script that generates the struct definition for each architecture.

    Also, if all your members have sizes that are powers of two, then you will get optimal packing simply by sorting members by size (largest first.) In that case, you can just use good old-fashioned macro-based struct-building – something like this:

    #define MYSTRUCT_POINTERS      \
        Something*  m_pSomeThing;  \
        OtherThing* m_pOtherThing; 
    
    #define MYSTRUCT_FLOATS        \
        FLOAT m_aFloat;            \
        FLOAT m_bFloat;
    
    #if 64_BIT_POINTERS && 64_BIT_FLOATS
        #define MYSTRUCT_64_BIT_MEMBERS MYSTRUCT_POINTERS MYSTRUCT_FLOATS
    #else if 64_BIT_POINTERS
        #define MYSTRUCT_64_BIT_MEMBERS MYSTRUCT_POINTERS
    #else if 64_BIT_FLOATS
        #define MYSTRUCT_64_BIT_MEMBERS MYSTRUCT_FLOATS
    #else
        #define MYSTRUCT_64_BIT_MEMBERS
    #endif
    
    // blah blah blah
    
    struct MyStruct
    {
        MYSTRUCT_64_BIT_MEMBERS
        MYSTRUCT_32_BIT_MEMBERS
        MYSTRUCT_16_BIT_MEMBERS
        MYSTRUCT_8_BIT_MEMBERS
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 184k
  • Answers 184k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Handling exceptions with the ELEHAB is basically the same in… May 12, 2026 at 4:52 pm
  • Editorial Team
    Editorial Team added an answer I would say that when setMessageDrivenContext is called, that's your… May 12, 2026 at 4:52 pm
  • Editorial Team
    Editorial Team added an answer dbUnit does not create tables. Nor could it with the… May 12, 2026 at 4:52 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS
I am currently running into a problem where an element is coming back from

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.