How to avoid structure padding in C?
Answer Posted / santhi perumal
Those are 3 different things.
Structure Padding
Most processors require specific memory alignment on
variables certain types. Normally the minimum alignment is
the size of the basic type in question, fo 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 boundarys 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 correect 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 appear 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.
Is This Answer Correct ? | 59 Yes | 2 No |
Post New Answer View All Answers
Is the exit() function same as the return statement? Explain.
Can we access the array using a pointer in c language?
Why do we need a structure?
Explain the difference between malloc() and calloc() function?
Write the program with at least two functions to solve the following problem. The members of the board of a small university are considering voting for a pay increase for their 10 faculty members. They are considering a pay increase of 8%. Write a program that will prompt for and accept the current salary for each of the faculty members, then calculate and display their individual pay increases. At the end of the program, print the total faculty payroll before and after the pay increase, and the total pay increase involved.
What does c mean?
Write a C program to help a HiFi’s Restaurant automate its breakfast billing system. Your assignment should implement the following items: a. Show the customer the different breakfast items offered by the HiFi’s Restaurant. b. Allow the customer to select more than one item from the menu. c. Calculate and print the bill to the customer. d. Produce a report to present your complete program and show more sample output. Assume that the HiFi’s Restaurant offers the following breakfast menu: Plain Egg $2.50 Bacon and Egg $3.45 Muffin $2.20 French Toast $2.95 Fruit Basket $3.45 Cereal $0.70 Coffee $1.50 Tea $1.80
Describe the complexity of Binary search, Quicksort and various other sorting and searching techniques..
What are disadvantages of C language.
What is type qualifiers?
How can I call system when parameters (filenames, etc.) Of the executed command arent known until run time?
What is linear search?
What is break statement?
Is c dynamically typed?
How old is c programming language?