Skip to main content

+1 866 653-6233 LinkedIn Software Expert Witness Directory

About Us Contact Us

Barr Group Software Experts

Barr Group Software Experts

Main navigation

  • Expert Services
    • Consulting Experts in Software and Electronics
    • Expert Reports by Testifying Software Experts
    • Reverse Engineering and Forensic Analysis
    • Software Source Code Review and Analysis
  • Areas of Expertise
    • Left Side
      • Artificial Intelligence
      • Automotive Systems
      • Cloud Computing
      • Computer Security
      • Consumer Electronics
      • Electronic Circuits
      • Enterprise Software
      • Financial Technology
      • Firmware and IoT
    • Right Side
      • Industrial Controls
      • Mechanical Design
      • Medical Devices
      • Military & Aerospace
      • Mobile Devices & Apps
      • Optical Equipment
      • Renewable Energy
      • Signal Processing
      • Telecommunications
  • Matters & Venues
    • Patent Infringement and Invalidity Experts
    • Software Copyright and Trade Secrets Experts
    • Product Liability and Failure Analysis Experts
    • Contract Disputes and Software Project Failures
    • Venues and Clients
  • Directory
  • Case Studies
    • DirecTV Anti-Piracy
    • Samsung Software Copyright
    • Toyota Runaway Cars
  • Resources
    • Expert Witness Blog
    • Source Code Review in Litigation
    • Software Source Code Discovery

5.5 Structures and Unions

  1. Home
  2. Embedded C Coding Standard
  3. 5 Data Type Rules
  4. 5.5 Structures and Unions

Rules:

5.5.a. Appropriate care shall be taken to prevent the compiler from inserting padding bytes within struct or union types used to communicate to or from a peripheral or over a bus or network to another processor.

5.5.b. Appropriate care shall be taken to prevent the compiler from altering the intended order of the bits within bit-fields.

Example:

typedef struct
{
    uint16_t  count;             // offset 0	
    uint16_t  max_count;         // offset 2	
    uint16_t  _unused;		 // offset 4	

    uint16_t  enable	  : 2;   // offset 6 bits 15-14
    uint16_t  b_interrupt : 1;   // offset 6 bit  13
    uint16_t  _unused1	  : 7;   // offset 6 bits 12-6
    uint16_t  b_complete  : 1;   // offset 6 bit  5
    uint16_t  _unused2	  : 4;   // offset 6 bits 4-1
    uint16_t  b_periodic  : 1;   // offset 6 bit  0

} timer_reg_t;

// Preprocessor check of timer register layout byte count.
#if ((8 != sizeof(timer_reg_t))
#   error “timer_reg_t struct size incorrect (expected 8 bytes)”
#endif

Reasoning: Owing to differences across processor families and loose definitions in the ISO C language standards, there is a tremendous amount of implementation- defined behavior in the area of structures and unions. Bit-fields, in particular, suffer from severe portability problems, including the lack of a standard bit ordering and no official support for the fixed-width integer types they so often call out to be used with. The methods available to check the layout of such data structures include static assertions or other compile-time checks as well as the use of preprocessor directives, e.g., to select one of two competing struct layouts based on the compiler.

Enforcement: These rules shall be enforced during code reviews.

Book traversal links for 5.5 Structures and Unions

  • ‹ 5.4 Floating Point
  • Up
  • 5.6 Booleans ›

Request an Expert

(866) 653-6233

Barr Group logo
Call us

Expert Services

  • Source Code Review Services
  • Expert Witness Directory
  • Reverse Engineering Services
  • Expert Reports & Testimony
  • How-To Technical Articles
  • Engineering Services

Latest Insights

  • Payment Processing and e-Payments Fraud
  • Albert Einstein Expert Witness
  • Medical Device Litigation and FDA 510(k)
  • Personality Traits of the Best Expert Witnesses

Website contents copyright © 2012-2025 by Barr Group. | Barr Group's logo is a U.S.-registered ® trademark.

SITEMAP  |  PRIVACY