Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8737263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:28:56+00:00 2026-06-13T10:28:56+00:00

Suppose I have the following structure: typedef struct { unsigned field1 :1; unsigned field2

  • 0

Suppose I have the following structure:

typedef struct
{
    unsigned field1 :1;
    unsigned field2 :1;
    unsigned field3 :1;
} mytype;

The first 3 bits will be usable but sizeof(mytype) will return 4 which means 29 bits of padding.
My question is, are these padding bits guaranteed by the standard to be zero initialized by the statement:

mytype testfields = {0};

or:

mytype myfields = {1, 1, 1};

Such that it’s safe to perform the following memcmp() on the assumption that bits 4..29 will be zero and therefore won’t affect the comparison:

if ( memcmp(&myfields, &testfields, sizeof(myfields)) == 0 )
    printf("Fields have no bits set\n");
else
    printf("Fields have bits set\n");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:28:57+00:00Added an answer on June 13, 2026 at 10:28 am

    Yes and no. The actual standard, C11, specifies:

    If an object that has static or thread storage duration is not
    initialized explicitly, then:

    • ….

    • if it is an aggregate, every member is initialized (recursively)
      according to these rules, and any padding is initialized to zero bits;

    So this only holds for objects of static storage, at a first view. But then later it says in addition:

    If there are fewer initializers in a brace-enclosed list than there
    are elements or members of an aggregate, or fewer characters in a
    string literal used to initialize an array of known size than there
    are elements in the array, the remainder of the aggregate shall be
    initialized implicitly the same as objects that have static storage
    duration.

    So this means that padding inside sub-structures that are not initialized explicitly is zero-bit initialized.

    In summarry, some padding in a structure is guaranteed to be zero-bit initialized, some isn’t. I don’t think that such a confusion is intentional, I will file a defect report for this.

    Older versions didn’t have that at all. So with most existing compilers you’d have to be even more careful, since they don’t implement C11, yet. But AFAIR, clang already does on that behalf.

    Also be aware that this only holds for initialization. Padding isn’t necessarily copied on assignment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following table structure: Field1 Field2 Field3 val1 valb_1 date val1
Suppose I have a following linked list structure: struct linked_list { struct linked_list *next;
Suppose I have the following code: typedef struct { char **p; } STRUCT; int
suppose you have the following structure: #include <windows.h> // BOOL is here. #include <stdio.h>
Suppose I have the following directory structure in Vim's NERDTree: /some/folder/ |-apples.txt |-bananas.txt |-
Suppose I have the following (desired) folder structure: *CommonProject *Project#1 ----> CommonProject(link) *Project#2 ---->
Suppose I have the following simple struct: struct Vector3 { double x; double y;
Suppose I have the following struct: struct sampleData { int x; int y; };
Suppose I have the following class structure: [XmlInclude(typeof(CustomNode))] [XmlInclude(typeof(CustomNode2))] [XmlRoot(node)] class Node { [XmlElement(node)]
Suppose I have the following directory structure: workspace/ __init__.py ys_manage/ __init__.py manage.py ys_utils/ __init__.py

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.