Thread: sizeof operator, im confused

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    31

    sizeof operator, im confused

    good day guys,

    i have this code

    Code:
    #include <stdio.h>
    
    
    int main(){
        struct a{
            int x;
            char y[10];
        };
        struct a aa;
        printf("%d", sizeof(aa));
    }
    when i run it the output is
    16
    when i change sizeof aa to sizeof aa.x the output is 4 which is understandable since member X is an int.

    when i changed it to sizeoff aa.y the output is 10 which is also understandable, however when its sizeof aa its output is 16.

    why??

    i read that by design, the memory allocated for each member is == to memory allocated for the largest member so in that case the sizeoff aa ought to return 20 since char y[10] is the largest member occupying 10bytes.

    what i'm i missing?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The size of a structure is the sum of the sizes of it's members, plus some optional padding necessary to preserve alignment. The amount of padding usually depends on the alignment of the largest primitive data type in the struct (int, char, float, double, pointer).
    Code:
    #include <stdio.h>
    #include <stddef.h>
    int main(){
        struct a{
            int x;
            char y[10];
        };
        struct b{
            char y[10];
            int x;
        };
        printf("%zd\n", sizeof(struct a));
        printf("%zd\n", sizeof(struct b));
    
        printf("%ld\n", offsetof(struct a,x));
        printf("%ld\n", offsetof(struct a,y));
    
        printf("%ld\n", offsetof(struct b,y));
        printf("%ld\n", offsetof(struct b,x));
    }
    
    $ ./a.out 
    16
    16
    0
    4
    0
    12
    In struct a, the padding is at the end of the structure.
    In struct b, the padding is between the array of chars and the int.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    SnoopFrogg KingFlippyNips's Avatar
    Join Date
    Sep 2016
    Posts
    33
    What does this do?
    Code:
     offsetof();

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by KingFlippyNips View Post
    What does this do?
    Code:
     offsetof();
    offsetof - cppreference.com
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    Quote Originally Posted by KingFlippyNips View Post
    What does this do?
    Code:
     offsetof();
    That's C++ not C though.

    Just thought I'd point that out as I thought "I've never seen that before", and now I know why (I don't know C++).

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by BpB View Post
    That's C++ not C though.

    Just thought I'd point that out as I thought "I've never seen that before", and now I know why (I don't know C++).
    I want to point out that this is still C. The person you are replying to simply used a C++ reference site.

    offsetof(3): offset of structure member - Linux man page

    On the above linked page you will also see, among other information, what standards this conforms to: C89, C99, POSIX.

    The reason you might not have encountered it would be it's not considered a beginner tool, i.e. it's reasonable you will write entire programs without it. I'm sure it's most common use is in the debugging area.

  7. #7
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    Quote Originally Posted by whiteflags View Post
    I want to point out that this is still C. The person you are replying to simply used a C++ reference site.

    offsetof(3): offset of structure member - Linux man page

    On the above linked page you will also see, among other information, what standards this conforms to: C89, C99, POSIX.

    The reason you might not have encountered it would be it's not considered a beginner tool, i.e. it's reasonable you will write entire programs without it. I'm sure it's most common use is in the debugging area.
    I see, right, thanks for that correction.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Hint: Search for "c++ keyword" returns good results for most C keywords; but you do have to know a little C/C++ to recognize when it is C++ code being shown.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Quote Originally Posted by stahta01 View Post
    Hint: Search for "c++ keyword" returns good results for most C keywords; but you do have to know a little C/C++ to recognize when it is C++ code being shown.

    Tim S.
    offsetof() is not a keyword, and is not "C++". It is a C function. Even if you don't have a "man" help system on your computer, a simple Google search for "man 3 offsetof" will show you the use of the "C" function, in the Section 3, "Library functions". "Section 2" contains the System calls. Please see the main page for "Linux man pages" for more information on all the sections.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    keyword was used as search term
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  11. #11
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Quote Originally Posted by stahta01 View Post
    Hint: Search for "c++ keyword" returns good results for most C keywords; but you do have to know a little C/C++ to recognize when it is C++ code being shown.

    Tim S.
    Tim:

    The word "keyword" has a very specific meaning in C and C++ We would never use "keyword" as a "search term". "keywords" and "functions" are two different things.

    Users here on the C forum, are beginners learning C, not C++. Asking them to search for a "c++ keyword" might, and probably will confuse them as they might not know anything about C++.
    but you do have to know a little C/C++ to recognize when it is C++ code being shown.
    Ditto! A C beginner shouldn't have to.

    The man system is very useful to both beginners, and experienced programmers. Better to recommend using man especially as there are several sites available online, such as die.net.

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Maybe at this point it should be noted that offsetof is neither a keyword nor a function, but a macro.

  13. #13
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    A "function" may take parameters and/or may return some value.

    A "function" can be implemented in the C language as:

    a regular function

    an inline function

    a macro function

    They are all used the same way, and are treated the same in the man system.

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Either precision in the language matters or it doesn't.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sizeof operator in c
    By Saurabh Mehta in forum C Programming
    Replies: 10
    Last Post: 01-27-2013, 08:48 PM
  2. sizeof operator
    By saswatdash83 in forum C Programming
    Replies: 2
    Last Post: 07-26-2010, 12:38 AM
  3. Replies: 6
    Last Post: 10-15-2007, 08:05 AM
  4. sizeof operator
    By Dave Jay in forum C Programming
    Replies: 9
    Last Post: 04-05-2005, 10:14 PM
  5. sizeof operator
    By Roaring_Tiger in forum C Programming
    Replies: 9
    Last Post: 09-05-2004, 07:31 AM

Tags for this Thread