• C

structure padding

what is structure padding?
who will take care of structure padding,compiler or programmer
what are the consequeses if structure padding is done?
prasad2315Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
I already started explaining this in your other thread, but I'll repeat it here :

>> what is structure padding?

Between the members of structs, there can exist padding. The standard does not define if and how puch padding there is, just that there can be. That's entirely platform/compiler dependent.

For example :

        typedef struct Test {
            char c;
            /* padding can exist here */
            short i[5];
            /* padding can exist here */
            double d;
            /* padding can exist here */
        } Test;

padding is just unused space that can contain any value.


>> who will take care of structure padding,compiler or programmer

The compiler will decide whether, where and how much padding will be inserted.

Often, compilers have ways of changing the packing behavior of structs (and thus also the padding). For example, some compilers support #pragma pack to change the alignment of members within a struct.
These are however compiler specific, and not portable.


>> what are the consequeses if structure padding is done?

The consequence is that you have to be aware that there can be padding, and can't rely on how the members of a struct will be aligned. One of the important consequences is what we discussed in your other question : when comparing struct instances, you can't reliably use memcmp, because that will also take into account the padding.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evilrixSenior Software Engineer (Avast)Commented:
Take a look at this Wikipedia page, which discusses it in some depth.
http://en.wikipedia.org/wiki/Data_structure_alignment
0
evilrixSenior Software Engineer (Avast)Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.