Thread: Understanding offsetof(struct ,member);

  1. #1
    Registered User thriller500's Avatar
    Join Date
    Oct 2011
    Posts
    25

    Understanding offsetof(struct ,member);

    Code:
    #include <stddef.h>
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {    
    struct s 
    {        int i;
            char c;  
          double d;  
          char a[];  
      };   /* Output is compiler dependent */ 
    
      printf("offsets: i=%ld; c=%ld; d=%ld a=%ld\n",(long) offsetof(struct s, i), (long) offsetof(struct s, c), (long) offsetof(struct s, d), (long) offsetof(struct s, a));   
     printf("sizeof(struct s)=%ld\n", (long) sizeof(struct s));
       exit(EXIT_SUCCESS);
    }
    Output : offsets: i=0; c=4; d=8 a=16sizeof(struct s)=16
    My Hypothesis :
    Integer is 4 bytes ..
    its first index so i=0;
    Character is 1 byte ..
    but d=8 it should be 5.Can someone explain me how offset works..
    Last edited by thriller500; 12-21-2012 at 03:20 AM. Reason: Aligning the code

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by thriller500 View Post
    Character is 1 byte ..but d=8 it should be 5.
    First off, I don't know why all your code is on one line, but if you want anyone to read it, make sure it's formatted correctly.

    offsetof() works as you think - it gives the offset, in bytes, of the given member of the structure. However, in a struct the compiler is allowed to lay out variables however it wants, including reordering variables and adding "padding" or unused bytes between members. This is usually to get good/needed alignment to make faster code. Basically, just trust what offsetof() says.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > including reordering variables
    This isn't true at all.
    Quote Originally Posted by C99
    Semantics
    4 As discussed in 6.2.5, a structure is a type consisting of a sequence of members, whose
    storage is allocated in an ordered sequence, and a union is a type consisting of a sequence
    of members whose storage overlap.
    The size of each member, and the padding between them is compiler dependent, but the order itself is fixed in the order written in the source code.

    Otherwise, you would never be able to initialise a struct if the compiler were free to rearrange the order.
    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.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    I didn't know that. Although, to be pedantic:

    Quote Originally Posted by C99
    Semantics
    4 As discussed in 6.2.5, a structure is a type consisting of a sequence of members, whose storage is allocated in an ordered sequence, and a union is a type consisting of a sequence of members whose storage overlap.
    Just because it's an ordered sequence doesn't imply its members have the same order in memory as in the definition - however, I see that a later point does state that this must be the case.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by JohnGraham View Post
    Just because it's an ordered sequence doesn't imply its members have the same order in memory as in the definition - however, I see that a later point does state that this must be the case.
    The reason the standard defines an order of members is that, before such a thing was standardised, compilers chose a defacto order and a lot of code came to rely on that order.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Indicate the member of the struct
    By Hannibal2010 in forum C Programming
    Replies: 15
    Last Post: 12-10-2011, 09:51 AM
  2. Understanding Struct
    By Oonej in forum C Programming
    Replies: 2
    Last Post: 07-12-2011, 02:17 PM
  3. Assign struct member value to struct member value
    By thahemp in forum C Programming
    Replies: 5
    Last Post: 10-13-2010, 09:48 AM
  4. struct member assignment (c-only)
    By emorrp1 in forum C Programming
    Replies: 8
    Last Post: 06-25-2008, 05:30 AM
  5. Replies: 1
    Last Post: 05-05-2004, 06:58 AM