Sitemap

Are Padding Bytes Really Unnecessary When Declaring a Structure?

4 min readMar 28, 2026

--

Understanding Memory Allocation of Structures (struct in low-level programming)

github.com

The source code to be referenced can be downloaded from the link above.

1. Problem Statement

2. Problem Analysis
1) Actual Memory Structure
2) What is a ‘Padding Byte’?
3) The Theory of Padding Byte Uselessness

3. Solution
1) The Usefulness of Padding Bytes
2) Application to Practice (Conclusion)

1. Statement of the Problem

When an embedded programmer (firmware developer) executes firmware (program) they have coded, if they know the order of the member variables of the structures allocated in memory by the compiler, they can also consider padding. In the figure below, padding refers to a kind of dummy memory space between the lines that the compiler uses to align them automatically. The sum of these remaining memory bytes is called ‘padding bytes’. So, are these padding bytes, which only take up memory space, really unnecessary?

Press enter or click to view image in full size
A structure where member variables are allocated in memory based on the largest member variable, grade (double data type).

* Firmware: ‘Software for hardware’ embedded in the ROM (memory) of the hardware to directly drive and control the device. It controls the hardware independently without an operating system (OS) in embedded PCs of ATMs, home appliances, IoT devices, etc., and allows for updates (or degrading via rollback) to add functions or correct errors.

2. Problem Analysis

1) Padding Byte Alignment

For example, in an ARM- 32-bit system (almost all modern systems are 64-bit), it has become a widely accepted theory that if different specific variables are not aligned sequentially within a standard memory size (unit), as in the memory structure where storage space is allocated below (Misaligned), the system may slow down.

Press enter or click to view image in full size
Source: https://dev.to/amanprasad/structure-padding-isnt-wastage-of-memory-its-a-hardware-requirement-2gk3

2) What is a ‘padding byte’?

Press enter or click to view image in full size

Looking at the source code above, the members of the declared struct appear simple. It consists of one int data field (4 bytes) and one double data field (8 bytes) . Manually adding the sizes results in 12 bytes. However, if you actually check the size of the entire structure, it is 16 bytes. This means that 4 bytes of padding are inserted in the middle.

3) The Padding Bite Uselessness Theory

The theory that communication can be slowed down because useless padding bytes inserted in the middle consume *packet size between the system or network transmission leads one to believe that padding is actually unnecessary. Therefore, there are cases where a header like the one below is added to avoid using padding from the start.

Press enter or click to view image in full size
The moment padding is removed using the #pragma pack(1) header setting…

Packing actually only affects the memory layout and does not affect CPU operation.

When packing a structure (pragma pack):

a. The padding bite is removed.

Get Younggi Seo (Daniel)’s stories in your inbox

Join Medium for free to get updates from this writer.

b. Multibyte fields can be misaligned.

c. The size of the struct becomes smaller. (Memory waste is eliminated)

Things that do not change:

a. How the CPU fetches memory

b. Architecture alignment requirements

c. Costs resulting from an incorrect approach

d. The CPU still expects aligned data, but packing simply removes the compiler’s safety mechanism.

So, padding is actually necessary.

Packets: A transmission unit consisting of small pieces of data sent over a computer network. Large data is divided into multiple packets like a parcel for transmission and reassembled at the destination; it consists of actual data (payload) and address information (header), enabling efficient network transmission.

3. Solution

1) The usefulness of padding bites

If you remove the padding, you may have to bear the following costs.

a. Multibyte fields can be misaligned.

b. The CPU may require multiple (*cycles) memory reads to read a single variable.

c. Additional instructions are required to assemble the values.

d . In some architectures, incorrect alignment access can cause traps or system crashes. In other words, while the number of bytes is saved, processing power is degraded.

*Cycles: The clock period or clock cycle is the basic unit of time for a computer’s CPU to operate, representing the time it takes for one clock signal (high-low) to complete. It is measured in picoseconds ( ) or nanoseconds ( ), and the shorter this period, the more instructions can be processed per second, thereby improving CPU performance.

--

--

Younggi Seo (Daniel)
Younggi Seo (Daniel)
0 followers

Cyber Security Consultant | Cloud Security (Azure, AKS) | SIEM (Microsoft Sentinel) | ISO 27001 / GRC