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?
 
Infinity08Connect With a Mentor Commented:
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
 
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
 
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
0
All Courses

From novice to tech pro — start learning today.