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

  • SEARCH
  • Home
  • 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 8282007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:13:34+00:00 2026-06-08T10:13:34+00:00

When I run sizeof(r) on my Mac. It says sizeof(r) = 1 . My

  • 0

When I run sizeof(r) on my Mac. It says sizeof(r) = 1. My understanding is that the size of a union is the size of its largest element. In this case shouldn’t the largest element be the struct s?

union
{
    struct
    {
        char i:1;
        char j:2;
        char m:3;
    }s;

    char ch;
}r;
  • 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-08T10:13:36+00:00Added an answer on June 8, 2026 at 10:13 am

    Your union composes of two parts, a struct, and a character. The size of the union, since it shares the memory, is the size of the largest element, plus the size of any padding it sticks on (which in your case is 0 bytes).

    First, let’s see the size ideone reports for each:
    http://ideone.com/LAhop

    Okay, both are 1. Therefore, the union’s size must be 1 as well.

    The struct is composed of bitfields. One is 1 bit, one is 2, and one is 3. This gives a total of 6 out of the 8 bits in one byte. Since it has to be at least one byte anyway (bitfields aren’t really sized in bits), the size is 1.

    As for char, here’s what the C++11 standard says in § 3.9.1/1 [basic.fundamental]:

    Objects declared as characters (char) shall be large enough to store any member 
    of the implementation’s basic character set.
    

    For pretty much every platform, this is one byte.
    This is one byte.

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

Sidebar

Related Questions

when i run this it says the size is 4 when it is really
When I run this code: #include <stdio.h> typedef struct _Food { char name [128];
When I run valgrind on this function, it says I've definitely lost 4 bytes.
I run this code here <html> <script type=text/javascript src=lib/jquery-ui-1.8.21.custom.min.js></script> <script src=http://127.0.0.1:5984/_utils/script/jquery.couch.js></script> <!--<script type=text/javascript src=lib/jquery-1.7.2.js></script>-->
I run into this problem. I have a textarea which I only want to
I'm just starting out on my first 'real' cocoa app. Note that this is
when i run this i get segv on printf, what am i doing wrong?
I've run into a EXC_BAD_ACCESS with a piece of code that deals with data
I am attempting to compile and run a test C program in Xcode. This
here i want to run this program and take one character pointer like char

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.