answersLogoWhite

0

What is structure padding?

Updated: 12/17/2022
User Avatar

Wiki User

7y ago

Best Answer

When you are using C, structure padding is used to pad the data in such a way that it can be sent to external devices. Sometimes, the data is padded in such a way that it can be used on little endian vs big endian processors.

Padding is done to fast access of data from memory. Structure members are aligned to based on the memory pointer size, which is normally 32bit in Win32 systems. So a character variable defined may take 4 bytes. Hence the size of structure is not really the sum of size individual members.

Extra bits between or after the structure's defined fields, that do not belong to any of those fields. The purpose of the padding is to position the fields on byte boundaries or even word boundaries, which is convenient for the computer. And the purpose of that is faster reading / writing / searching.

Answer:

Most processors require specific memory alignment on variables certain types. Normally the minimum alignment is the size of the basic type in question, for instance this is common.

Char variables can be byte aligned and appear at any byte boundary

Short (2 byte) variables must be 2 byte aligned, they can appear at any even byte boundary. This means that 0x10004567 is not a valid location for a short variable but 0x10004566 is.

long (4 byte) variables must be 4 byte aligned, they can only appear at byte boundaries that are a multiple of 4 bytes. This means that 0x10004566 is not a valid location for a long variable but 0x10004568 is.

Structure padding occurs because the members of the structure must appear at the correct byte boundary, to achieve this the compiler puts in padding bytes (or bits if bit fields are in use) so that the structure members appear in the correct location. Additionally the size of the structure must be such that in an array of the structures all the structures are correctly aligned in memory so there may be padding bytes at the end of the structure too.

struct example {

char c1;

short s1;

char c2;

long l1;

char c3;

}

In this structure, assuming the alignment scheme I have previously stated then

c1 can appear at any byte boundary, however s1 must appear at a 2 byte boundary so there is a padding byte between c1 and s1.

c2 can then appear in the available memory location, however l1 must be at a 4 byte boundary so there are 3 padding bytes between c2 and l1

c3 then appears in the available memory location, however because the structure contains a long member the structure must be 4 byte aligned and must be a multiple of 4 bytes in size. Therefore there are 3 padding bytes at the end of the structure. it would appear in memory in this order

c1

padding byte

s1 byte 1

s1 byte 2

c2

padding byte

padding byte

padding byte

l1 byte 1

l1 byte 2

l1 byte 3

l1 byte 4

c3

padding byte

padding byte

padding byte

The structure would be 16 bytes long.

re-written like this

struct example {

long l1;

short s1;

char c1;

char c2;

char c3;

}

Then l1 appears at the correct byte alignment, s1 will be correctly aligned so no need for padding between l1 and s1. c1, c2, c3 can appear at any location. The structure must be a multiple of 4 bytes in size since it contains a long so 3 padding bytes appear after c3

It appears in memory in the order

l1 byte 1

l1 byte 2

l1 byte 3

l1 byte 4

s1 byte 1

s1 byte 2

c1

c2

c3

padding byte

padding byte

padding byte

and is only 12 bytes long.

I should point out that structure packing is platform and compiler (and in some cases compiler switch) dependent.

Memory Pools are just a section of memory reserved for allocating temporarily to other parts of the application

A memory leak occurs when you allocate some memory from the heap(or a pool) and then delete all references to that memory without returning it to the pool it was allocated from.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is structure padding?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Why need a structure padding in structure?

structure padding matters most when alingnment matters, which helps in potential performance gain and avoiding possible bus errors, please refer http://www.refcode.net/2013/02/structure-padding-explained.html for detailed explanation


Why need a structure padding in c?

to satisfy structre member alignment rules structure padding needed when alignment matters, which is useful for performance and avoids potential bus errors when alignment is a must on a particular architecture, please refer http://www.refcode.net/2013/02/structure-padding-explained.html for detailed explanation


How to overcome structure padding?

I think we can use #pragma pack(1) By this the structure will he paked in 1 byte order so there will be no waste of memory .


Is there a population chart that shows fity years of population in New York?

{| ! style="PADDING-RIGHT: 1px; PADDING-: 1px; PADDING-BOTTOM: 1px; PADDING-TOP: 1px" | 1950 | 7,891,957 ! style="PADDING-RIGHT: 1px; PADDING-: 1px; PADDING-BOTTOM: 1px; PADDING-TOP: 1px; BORDER-BOTTOM: #bbbbbb 1px solid" | 1960 | 7,781,984 ! style="PADDING-RIGHT: 1px; PADDING-: 1px; PADDING-BOTTOM: 1px; PADDING-TOP: 1px" | 1970 | 7,894,862 ! style="PADDING-RIGHT: 1px; PADDING-: 1px; PADDING-BOTTOM: 1px; PADDING-TOP: 1px" | 1980 | 7,071,639 ! style="PADDING-RIGHT: 1px; PADDING-: 1px; PADDING-BOTTOM: 1px; PADDING-TOP: 1px" | 1990 | 7,322,564 ! style="PADDING-RIGHT: 1px; PADDING-: 1px; PADDING-BOTTOM: 1px; PADDING-TOP: 1px" | 2000 | 8,008,288 |}


What are the benefits of fats?

of the top of my head- protection (by providing padding when injured)- thermoregulation (particularly in babies)- storage (of energy)- cholesterol specifically is involved in maintaining the structure of the structure of the phospholipid bylayer.


Can Superman turn into a super nova?

It is not one of his powers but briefly in an issue when he was depowered, Superman wore a powersuit and was called Supernova. {| id="ezSearchBarTABLE" class="ezui-corner-all" style="background-color: #ffe; border: 2px solid #555; padding: 1px; z-index: 9999999; display: block; vertical-align: bottom" |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 0px" | |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 4px" | | style="padding: 0; padding-right: 0px" | |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 0px" | |} +1[[#|close]] [[#|close]][[#|pin]][[#|pin]][[#|search]][[#|minimize]][[#|forward]][[#|back]][[#|ViewText]][[#|]][[#|]][[#|zoomout]][[#|zoomin]][[#|zoomin]] {| id="ezSearchBar" style="border: 0; display: none; vertical-align: bottom; padding-bottom: 2px" | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | |}


Where can you download Issue?

If you mean comics issues, you can buy a subscription to dc comics and have them available for digital download. {| id="ezSearchBarTABLE" class="ezui-corner-all" style="background-color: #ffe; border: 2px solid #555; padding: 1px; z-index: 9999999; display: block; vertical-align: bottom" |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 0px" | |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 4px" | | style="padding: 0; padding-right: 0px" | |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 0px" | |} +1[[#|close]] [[#|close]][[#|pin]][[#|pin]][[#|search]][[#|minimize]][[#|forward]][[#|back]][[#|ViewText]][[#|]][[#|]][[#|zoomout]][[#|zoomin]][[#|zoomin]] {| id="ezSearchBar" style="border: 0; display: none; vertical-align: bottom; padding-bottom: 2px" | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | |}


Who was Oliver Daddy Warbucks?

He was a character in the comicstrip Little Orphan Annie.{| id="ezSearchBarTABLE" class="ezui-corner-all" style="background-color: #ffe; border: 2px solid #555; padding: 1px; z-index: 9999999; display: block; vertical-align: bottom" |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 0px" | |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 4px" | | style="padding: 0; padding-right: 0px" | |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 0px" | |}+1[[#|close]][[#|close]][[#|pin]][[#|pin]][[#|search]][[#|minimize]][[#|forward]][[#|back]][[#|ViewText]][[#|]][[#|]][[#|zoomout]][[#|zoomin]][[#|zoomin]]{| id="ezSearchBar" style="border: 0; display: none; vertical-align: bottom; padding-bottom: 2px" | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | |}


What are the main Batman villain's ethnic backrounds?

Most of them do not have confirmed or canon background stories. Bane is of British and Hispanic descent, his mother was from South America, and his father, King Snake, was British. {| id="ezSearchBarTABLE" class="ezui-corner-all" style="background-color: #ffe; border: 2px solid #555; padding: 1px; z-index: 9999999; display: block; vertical-align: bottom" |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 0px" | |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 4px" | | style="padding: 0; padding-right: 0px" | |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 0px" | |} +1[[#|close]] [[#|close]][[#|pin]][[#|pin]][[#|search]][[#|minimize]][[#|forward]][[#|back]][[#|ViewText]][[#|]][[#|]][[#|zoomout]][[#|zoomin]][[#|zoomin]] {| id="ezSearchBar" style="border: 0; display: none; vertical-align: bottom; padding-bottom: 2px" | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | |} Joker: Nothing confirmed Penguin: Same as joker but in a couple incarnations and games, he was British Two-Face: American Bane: Most likely of South-American descent {| id="ezSearchBarTABLE" class="ezui-corner-all" style="background-color: #ffe; border: 2px solid #555; padding: 1px; z-index: 9999999; display: block; vertical-align: bottom" |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 0px" | |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 4px" | | style="padding: 0; padding-right: 0px" | |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 0px" | |} +1[[#|close]] [[#|close]][[#|pin]][[#|pin]][[#|search]][[#|minimize]][[#|forward]][[#|back]][[#|ViewText]][[#|]][[#|]][[#|zoomout]][[#|zoomin]][[#|zoomin]] {| id="ezSearchBar" style="border: 0; display: none; vertical-align: bottom; padding-bottom: 2px" | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | |}


Who is more famous Incredible Hulk or LeBron James?

Thats an objective question, Hulk is better know to people who read comics, LeBron James is known to sports fans. Both of them have been featured outside their spheres of influence. {| id="ezSearchBarTABLE" class="ezui-corner-all" style="background-color: #ffe; border: 2px solid #555; padding: 1px; z-index: 9999999; display: block; vertical-align: bottom" |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 0px" | |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 4px" | | style="padding: 0; padding-right: 0px" | |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 0px" | |} +1[[#|close]] [[#|close]][[#|pin]][[#|pin]][[#|search]][[#|minimize]][[#|forward]][[#|back]][[#|ViewText]][[#|]][[#|]][[#|zoomout]][[#|zoomin]][[#|zoomin]] {| id="ezSearchBar" style="border: 0; display: none; vertical-align: bottom; padding-bottom: 2px" | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | |}


Where the list of guild in Free Realms?

There is no official list but rather a bunch of threads on various free realms forums that try to make a decent sized list of guilds. Just look them up and try combining lists. {| id="ezSearchBarTABLE" class="ezui-corner-all" style="background-color: #ffe; border: 2px solid #555; padding: 1px; z-index: 9999999; display: block; vertical-align: bottom" |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 0px" | |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 4px" | | style="padding: 0; padding-right: 0px" | |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 0px" | |} +1[[#|close]] [[#|close]][[#|pin]][[#|pin]][[#|search]][[#|minimize]][[#|forward]][[#|back]][[#|ViewText]][[#|]][[#|]][[#|zoomout]][[#|zoomin]][[#|zoomin]] {| id="ezSearchBar" style="border: 0; display: none; vertical-align: bottom; padding-bottom: 2px" | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | |}


How can a person from Europe play League of Legends in the PH server?

If you mean the pbe testing server, then yes you can. All of Riot's servers can be played anywhere but it is recommended to play on one located near where you live. {| id="ezSearchBarTABLE" class="ezui-corner-all" style="background-color: #ffe; border: 2px solid #555; padding: 1px; z-index: 9999999; display: block; vertical-align: bottom" |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 0px" | |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 4px" | | style="padding: 0; padding-right: 0px" | |- style="padding: 0" | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 2px" | | style="padding: 0; padding-right: 0px" | |} +1[[#|close]] [[#|close]][[#|pin]][[#|pin]][[#|search]][[#|minimize]][[#|forward]][[#|back]][[#|ViewText]][[#|]][[#|]][[#|zoomout]][[#|zoomin]][[#|zoomin]] {| id="ezSearchBar" style="border: 0; display: none; vertical-align: bottom; padding-bottom: 2px" | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | | style="padding-right: 4px" | |}