1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Output of program

Discussion in 'C++' started by SATYAN JANA, Sep 30, 2005.

  1. SATYAN JANA

    SATYAN JANA New Member

    Joined:
    Jul 31, 2004
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Programming
    Location:
    Kolkata
    Code:
    /******************************/
    class A
    {
    private:
      int a;
      char b;
    };
    
    class B
    {
    private:
      char b;
    };
    
    int main()
    {
      A a;
      B b;
      cout<<sizeof(a)<<"  "<<sizeof(b)<<endl;
      return 0;
    }
    /******************************************/
    
    What will be the output of the above program?
    The output will be,-
    8 1
    Why?...
















    In class B, 1 byte is as usual allocated for char 'b'. But in case of class A,
    4 bytes for integer 'a' and next 4 bytes for char 'b'. This is because of packaging format.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    I didn't knew this. Its really kool so out of excitement I tried even the double one and it gave 16 and 1
     
  3. SATYAN JANA

    SATYAN JANA New Member

    Joined:
    Jul 31, 2004
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Programming
    Location:
    Kolkata
    Hmmm,
    even the size of pointers can change. If you have double and char* still it will be (8 + 8 =)16.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    Yup, seen that.
     
  5. rahul_scss

    rahul_scss New Member

    Joined:
    Oct 15, 2005
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Can somebody please tell me what exactly the rules of this packaging format are???
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    If you visualize the memory as rows and columns then if you need to allocate some memory in different rows[for different variables] then you have to allocate n no of cols of bytes for each variable and n is choosen as maximum size.

    e.g.
    Variable 1 mem [8 bytes] | | | | | | | |
    Variable 2 mem [4 bytes] | | | |

    Instead of 4 bytes it allocates 8 to keep the packaging format in good shape for second variable memory.

    If you go about by sequential memory segment then also allocating each variable memory of n [choosen as maximum] is easier and so it allocates 8 + 8 bytes instead of 8 + 4 bytes.
     
    Last edited: Oct 17, 2005
  7. rahul_scss

    rahul_scss New Member

    Joined:
    Oct 15, 2005
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Thanks Very Much Shabbir...
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    My pleasure.
     
  9. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Very useful piece of information Shabbir bhai.
     

Share This Page