alignment and padding in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sangeeth
    New Member
    • Dec 2006
    • 23

    #1

    alignment and padding in C

    assume following sizes
    char 1 byte
    int 4 bytes
    short 2 bytes

    typedef struct
    {
    char name[30];
    int epno;
    int salary;
    }EmpRec, *EmpRecPtr;

    main()
    {
    int x =1;
    char y = 2;
    int z=3;
    EmpRec abc;
    EmpRecPtr empr = &abc;

    printf("Address : &x=%p,&y=%p, %z=%p, &abc = %p,&empr = %p\n",&x,&y,&z, &abc,&empr);
    }


    output is:

    Address : &x=0x22ccdc &y = 0x22ccdb, &z = 0x22ccd8, &abc=0x22cca 0 &empr=0x22cc 9c

    why is it so???? can any one help me
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    I don't really see the problem (other than z, and if that's your problem I have missed something...... ....)

    x has been allocated 0x22ccdc,0x22cc dd,0x22ccde, 00x22ccdf = 4bytes
    y has been allocated 0x22ccdb (before x) = 1 bvyte
    z has been allocated 0x22ccd8, 0x22ccd9, 0x22ccda, (which means either something is written wrong, or I did something blatantly obvious....I'll have a think if you confirm these are correct)
    abc has been allocated 0x22cca0, 0x22cca1, 0x22cca2, 0x22cca3,... 0x22ccd8 (which presumably is enough room for the struct (which should be around 38 bytes)
    empr has allocated 0x22cc9c, 0x22cc9d, 0x22cc9e, 0x22cc9f (which is an int to store the pointer)

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Since you have not declared main

      int main()
      {
      ...
      return 0;
      }

      your program invokes undefined behaviour and you have not garuntee that the printed values have any meaning or connection with reality.

      However assuming they are the only problem I see is that

      &y = 0x22ccdb
      &z = 0x22ccd8

      i.e. &y - &z = 3 which means there on only 3 bytes of memory available for the int z which you have said is a 4 byte value. My guess is that either the printed numbers are not correct or that you are wrong int is a 2 byte value.

      Comment

      • sangeeth
        New Member
        • Dec 2006
        • 23

        #4
        Originally posted by DeMan
        I don't really see the problem (other than z, and if that's your problem I have missed something...... ....)

        x has been allocated 0x22ccdc,0x22cc dd,0x22ccde, 00x22ccdf = 4bytes
        y has been allocated 0x22ccdb (before x) = 1 bvyte
        z has been allocated 0x22ccd8, 0x22ccd9, 0x22ccda, (which means either something is written wrong, or I did something blatantly obvious....I'll have a think if you confirm these are correct)
        abc has been allocated 0x22cca0, 0x22cca1, 0x22cca2, 0x22cca3,... 0x22ccd8 (which presumably is enough room for the struct (which should be around 38 bytes)
        empr has allocated 0x22cc9c, 0x22cc9d, 0x22cc9e, 0x22cc9f (which is an int to store the pointer)
        Well my query was based on a question in a book . .
        Anyway I ran the program and got the result as
        Address: &x=0xbfeb0bf 4, &y=0xbfeb0bf 3, &z=0xbfeb0be c, &abc=0xbfeb0bc4 , &empr=0xbfeb0bc 0

        which is self-explanatory . .. .
        One may note that values are placed 4 bytes aligned. . that is the reason for a 3 byte space after &y ..
        thanks all

        Comment

        • sangeeth
          New Member
          • Dec 2006
          • 23

          #5
          Well my query was based on a question in a book . .
          Anyway I ran the program and got the result as
          Address: &x=0xbfeb0bf 4, &y=0xbfeb0bf 3, &z=0xbfeb0be c, &abc=0xbfeb0bc4 , &empr=0xbfeb0bc 0

          which is self-explanatory . .. .
          One may note that values are placed 4 bytes aligned. . that is the reason for a 3 byte space after &y ..
          thanks all
          i.e. &y - &z = 3 which means there on only 3 bytes of memory available for the int z which you have said is a 4 byte value. My guess is that either the printed numbers are not correct or that you are wrong int is a 2 byte value.[/QUOTE]

          Comment

          Working...