Structure padding and packing

how to avoid structure padding in c
structure padding in c quora
struct __attribute__((packed))
c++ packed struct
python struct padding
golang struct packing
c struct no padding
add padding to struct


struct mystruct_A
   char a;
   int b;
   char c;
} x;

struct mystruct_B
   int b;
   char a;
} y;

The sizes of the structures are 12 and 8 respectively.

Are these structures padded or packed?

When does padding or packing take place?

Padding aligns structure members to "natural" address boundaries - say, int members would have offsets, which are mod(4) == 0 on 32-bit platform. Padding is on by default. It inserts the following "gaps" into your first structure:

struct mystruct_A {
    char a;
    char gap_0[3]; /* inserted by compiler: for alignment of b */
    int b;
    char c;
    char gap_1[3]; /* -"-: for alignment of the whole struct in an array */
} x;

Packing, on the other hand prevents compiler from doing padding - this has to be explicitly requested - under GCC it's __attribute__((__packed__)), so the following:

struct __attribute__((__packed__)) mystruct_A {
    char a;
    int b;
    char c;

would produce structure of size 6 on a 32-bit architecture.

A note though - unaligned memory access is slower on architectures that allow it (like x86 and amd64), and is explicitly prohibited on strict alignment architectures like SPARC.

Structure padding in C, is the extra space added to match the alignment. Padding and packing are just two aspects of the same thing: packing or alignment is the size to which each member is rounded off padding is the extra space added to match the alignment

structure padding and packing in c example-Firmware Developer, What do you mean by structure member alignment padding and data packing? The concept of avoiding the wastage of memory space by eliminating the concept of padding is called Structure packing. By default, padding is done by the compiler if we do packing, padding is removed. This may lead to more CPU cycles but memory is saved. This is done when there are memory constraints.

I know this question is old and most answers here explains padding really well, but while trying to understand it myself I figured having a "visual" image of what is happening helped.

The processor reads the memory in "chunks" of a definite size (word). Say the processor word is 8 bytes long. It will look at the memory as a big row of 8 bytes building blocks. Every time it needs to get some information from the memory, it will reach one of those blocks and get it.

As seem in the image above, doesn't matter where a Char (1 byte long) is, since it will be inside one of those blocks, requiring the CPU to process only 1 word.

When we deal with data larger than one byte, like a 4 byte int or a 8 byte double, the way they are aligned in the memory makes a difference on how many words will have to be processed by the CPU. If 4-byte chunks are aligned in a way they always fit the inside of a block (memory address being a multiple of 4) only one word will have to be processed. Otherwise a chunk of 4-bytes could have part of itself on one block and part on another, requiring the processor to process 2 words to read this data.

The same applies to a 8-byte double, except now it must be in a memory address multiple of 8 to guarantee it will always be inside a block.

This considers a 8-byte word processor, but the concept applies to other sizes of words.

The padding works by filling the gaps between those data to make sure they are aligned with those blocks, thus improving the performance while reading the memory.

However, as stated on others answers, sometimes the space matters more then performance itself. Maybe you are processing lots of data on a computer that doesn't have much RAM (swap space could be used but it is MUCH slower). You could arrange the variables in the program until the least padding is done (as it was greatly exemplified in some other answers) but if that's not enough you could explicitly disable padding, which is what packing is.

Understanding of structure padding in C with alignment, at a memory address equal to some multiple of the word size, which increases the system's performance due to the way the CPU handles memory. Why member alignment, padding and data packing is required in C structures? Data members in structures are aligned to make memory accesses faster. Padding is the technique in which compiler tries to maintain proper alignment of data elements by inserting unused memory between elements. Padding improves performance at expense of memory.

Structure Member Alignment, Padding and Data Packing , or union data members aligned as per the size of the highest bytes member to prevent the penalty of performance. Data Packing.As we can see padding does the lots of memory waste whether it increases the performance.Some of the processor does much focus of memory size rather than its performance then they restrict alignment or use methods like #pragma to decide the own alignment boundary to reduce padding effect.This customize packing of data into memory is known as data packing.

Data structure alignment, Structure Padding: In C/C++ a structures are used as data pack. It doesn't provide any data encapsulation  What is Packing. Packing, on the other hand prevents compiler from doing padding means remove the unallocated space allocated by structure. In case of Linux we use __attribute__((__packed__)) to pack structure. In case of Windows (specially in Dev c++) use # pragma pack (1) to pack structure. Example:-1. In case of Padding of Structure. Program

Structural Padding & Packing In C & C++, Alternately, one can pack the structure, omitting the padding, which may lead to slower access, but uses half as much  Depending on the CPU architecture and the compiler, a structure may occupy more space in memory than the sum of the sizes of its component members. The compiler can add padding between members or at the end of the structure, but not at the beginning. Packing overrides the default padding.

The Lost Art of Structure Packing, In order to align the data in memory, one or more empty bytes (addresses) are inserted (or left empty) between memory addresses which are allocated for other​  actual purpose of talking about padding and packing outside structures is to prepare you for what happens inside them. Here’s what actually happens (on an x86 or ARM or anything else with self-aligned types). The storage for pstarts on a self-aligned 4-

C Language - Structure Padding and Packing, What Is Structural Padding And Packing In C And C++ is very The idea is when we create Duration: 15:28 Posted: 25 Apr 2017 Data structure alignment refers to the way data is arranged and accessed in computer memory. It consists of three separate but related issues: data alignment, data structure padding, and packing. The CPU in modern computer hardware performs reads and writes to memory most efficiently when the data is naturally aligned, which generally means that the data's memory address is a multiple of the data size. For instance, in a 32-bit architecture, the data may be aligned if the data is stored in four

  • Read…
  • The Lost Art of C Structure Packing -
  • padding makes things bigger. packing makes things smaller. Totally different.
  • I wonder: is prohibition of unaligned memory on the spark means that it can not deal with an usual byte arrays? Struct packing as I know mostly used in transmitting(i.e networking) a data, when you need to cast a byte array to a struct, and be sure that an array fit to a struct fields. If the spark can not do that, how those working at all?!
  • That's exactly why, if you look at IP, UDP, and TCP header layouts, you'd see that all integer fields are aligned.
  • The "Lost Art of C Structure Packing" explains padding and packing ptimisations -
  • Does first member have to come first? I thought arragement is totally up to the implementation, and cannot be relied upon (even from version to version).
  • +allyourcode The standard guarantees that the order of the members will be preserved and that the first member will start at 0 offset.
  • "Rules" actually made it very clear, I couldn't find straightforward rule anywhere. Thanks.
  • @PervezAlam The book <The Lost Art of C Structure Packing>, explains the rules pretty well, even thought it's a little longer than this answer. The book is available freely online:
  • I will give it a try, btw is it limited to Structure packing? Just curios as I liked the explanation in book.
  • @PervezAlam It's a very short book, mainly focus on technology that would reduce memory footprint of c program, it takes only at most several days to finish reading.
  • @ValidusOculus Yes, it means 16 byte aligned.
  • This doesn't explain structure packing but it illustrates CPU word alignment quite nicely.