Unnecessary padding
Members of a struct are padded to fulfill alignment requirement
      when rearranging the members to fulfill this requirement saves memory
Since R2021b
Description
This checker flags a struct object where the arrangement of its members
      necessitates additional padding to fulfill alignment requirement. Rearranging the members of
      such a struct object might fulfill the alignment requirement without
      requiring any additional padding. Because the padding is unnecessary for alignment purposes,
      eliminating the padding saves memory. Consider this struct in a 64bit
      system:
 struct A {
      uint32_t m1;// 4 bytes
      uint64_t m2;// 8 bytes
      uint32_t m3;// 4 bytes
  }; 
 m1 and
        m2 are placed consecutively, the machine requires two cycles to read
        m2. Instead, the variable m1 is placed in a 8 byte
      slot by itself after padding it by 4 bytes. Then m2 is placed in its own 8
      byte slot. The variable m3 is also padded to fulfill alignment requirement
      for the struct A. Because of the padding, the size of A
      is 24 bytes even though the combined size of m1, m2, and
        m3 is 16.Polyspace® raises this defect when alignment requirement can be fulfilled by rearranging
      the members of a struct. For instance, rearranging the members of
        A can eliminate
      padding:
 struct A {
      uint64_t m2;// 8 bytes
      uint32_t m1;// 4 bytes
      uint32_t m3;// 4 bytes
  }; 
 m2 is placed first in a 8 byte slot. Then m1 and
        m3 are placed together in another 8 byte slot. This rearrangement
      eliminates the padding.Risk
Unnecessary padding wastes memory, which can have several adverse impacts:
- Using more memory than necessary might exhaust the available memory, resulting in paging fault. 
- Functions such as - memcpyand- memcmpmight take longer.
Fix
To fix this defect, rearrange the members of the struct to eliminate
        the unnecessary padding. Declare the largest struct members first, and
        then keep the declarations of same-sized members together. You might also use
          pragma directives to eliminate padding.
Performance improvements might vary based on the compiler, library implementation, and environment that you are using.
Examples
Result Information
| Group: Performance | 
| Language: C | C++ | 
| Default: Off | 
| Command-Line Syntax: UNNECESSARY_STRUCT_PADDING | 
| Impact: Medium | 
Version History
Introduced in R2021b
See Also
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)