Offset of a data member

Discussion in 'C' started by mr.anandc, May 7, 2008.

  1. mr.anandc

    mr.anandc New Member

    Joined:
    May 6, 2008
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    The below macro is defined in stddef.h standard header file.
    #define offsetof(s,m) (size_t)&(((s *)0)->m)

    This gives offset of member data 'm' in structure 's'

    Ex:
    struct example {
    int a;
    int b;
    };

    printf("%d", offsetof(example, b) ) displays 4 (In win32).

    Can any one explain how offsetof macro is working?

    Thanks.
     
  2. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    (size_t)&(((s *)0)->m)


    (s*)(0) = 0 is integer which is cast to s pointer
    ((s*)(0))->m = Data member access
    &(((s*)(0))->m) = Address of data member m
    (size_t)&(((s*)(0))->m) = cast to standard integer(unsigned)
     
  3. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    You can see this also...
    struct Data *ptr = 0;

    printf("%d\n",&(ptr->b));
     
  4. mr.anandc

    mr.anandc New Member

    Joined:
    May 6, 2008
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    Here my doubt is, if ptr value is 0 which is NULL, why access to b using ptr (ptr->b) is not crashing :confused: .

    Thanks.
     
  5. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA

    That is Now your Intelligent question!!!
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define offsetof(s,m) (size_t)&(((s *)0)->m)
    #define Off(i,j)  ((i*)0)->j
    
    
    struct Data
    {
     int a;
     int b;
    };
    
    int main()
    {
      int OffSet=offsetof(Data, b);
      printf("%d", OffSet) ;
      return 0;
    }
    
    In above  code , You are only getting the address not accessing.
    
    
    int main()
    { 
      int Add=Off(Data , b);
      printf("%d", Add) ;
      return 0;
    }
    In this code , now you are accessing so segmentation fault will come.
     
    Last edited by a moderator: May 9, 2008
  6. mr.anandc

    mr.anandc New Member

    Joined:
    May 6, 2008
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    Not yet cleared.
    Here how it is able differentiate between accessing address and accessing a member.
    Suppose in below statement
    #define offsetof(s,m) (size_t)&(((s *)0)->m)

    It is accessing address only. But before address operator, it is accessing member using ((s *)0)->m. How this is different from ((i*)0)->j.
     
  7. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    There is no difference b/w ((s *)0)->m and ((i*)0)->j .

    But when you write statement (size_t)&(((s *)0)->m)
    Then It will not go for accessing the member data. It's not like intermediate step ((s *)0)->m.
    just address .

    If you have doubt then tell me?
     
  8. mr.anandc

    mr.anandc New Member

    Joined:
    May 6, 2008
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    Then how compiler will differentiate between the two statements, so that it accesses only address of a member.
     
  9. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    there is differencce b/w &i and i.

    &i means not accessing i only address of i. compiler knows &i and i difference
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice