What would be the result...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sreeramu
    New Member
    • Apr 2007
    • 11

    What would be the result...

    Hi friends at present i am not having any compiler so please can any one conform the result of this program
    what would be the size of this structure

    Code:
    struct str 
    { 
    int data; 
    struct str *pstr; 
    chap ch; 
    int data1; 
    };
    what would be the size of this structure...
  • Studlyami
    Recognized Expert Contributor
    • Sep 2007
    • 464

    #2
    In VS 2005 the size is 16, but i believe this is system/compiler dependent so there could be different answers.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by Sreeramu
      Hi friends at present i am not having any compiler so please can any one conform the result of this program
      what would be the size of this structure

      Code:
      struct str 
      { 
      int data; 
      struct str *pstr; 
      chap ch; 
      int data1; 
      };
      what would be the size of this structure...
      We don't know because we don't know the size of ints and chars on your system,
      nor do we know the size of pointers nor the alignment requirements of your system.

      kind regards,

      Jos

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by JosAH
        we don't know the size of <snip> chars on your system
        In fairness since the size of everything is measured in units of char the size of char will not effect the size of a structure (or anything else) only the min/max values of the integers on the system and I fuess possibly the representation of floats and doubles.

        Comment

        • Sreeramu
          New Member
          • Apr 2007
          • 11

          #5
          16 is correct for int it would take 4 bytes and for char 4 bytes(due to structure padding) and the pointer 4 bytes so 4+4+4+4=16 ....

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by Sreeramu
            16 is correct for int it would take 4 bytes and for char 4 bytes(due to structure padding) and the pointer 4 bytes so 4+4+4+4=16 ....
            You need to read post 3 much more carefully and understand what it is saying, I have worked/do work on a system where the correct answer is 7 and another where the answer is 8.

            Comment

            Working...