size of structure member

Posted on 2011-10-16
Last Modified: 2012-05-12
I know that sizeof() doesn't necessarily return the sum of the sizes for all the structure members because of byte alignment issues.  However, does sizeof() return the correct size when called on the individual members of the structure?  Is this portable?

typedef struct {
    uint8_t a;
    uint16_t b;
    uint32_t c;
} mystruct;

mystruct mystruct;
size_t size;

size = sizeof(mystruct.b)

Will size always equal 2 bytes?


Question by:JohnSantaFe
    LVL 4

    Accepted Solution

    Genereally yes, but you should consider the following:

    1. The uint16_t type is only required to exist when there is a native type that is exactly 16 bits long.

    2. The sizeof operator returns size in chars, not bytes. The char type itself is only required to be at least 8 bits, but not exactly.

    Therefore, on some rare and crazy, but nevertheless conforming to the standard, architectures, you may get the following problems:

    1. If there is no native 16-bit type, it may even not compile.

    2. If char size is more than 8 bits, you won't get the size in bytes (octets).

    I once tried to figure out if any of such architectures actually exist, but was unable to found any. However, I can imagine some sort of special computer where char is 32 or 64 bits, and there are simply no smaller types supported by the CPU.
    LVL 8

    Assisted Solution

    > size = sizeof(mystruct.b)
    > Will size always equal 2 bytes?

    int8_t, int16_t and int32_t are optional types.
    So, your OS may define them or may not define them and hence they are not portable.

    Look at the page# 256 of the ISO standard regarding this:

    Author Closing Comment

    Thank you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now