Why in C++, Empty structure has size 1 byte and In C, 0 byte?

Discussion in 'C++' started by imrantechi, Mar 17, 2009.

  1. imrantechi

    imrantechi New Member

    Joined:
    Feb 12, 2008
    Messages:
    116
    Likes Received:
    4
    Trophy Points:
    0
    In C++ , size of empty structure is 1 byte where in C, size of structure is zero?

    As per my knowledge, If you will take array of n objects of any structure then where there distinguish so they have taken size atleast one byte, But this rule should apply to same in C also. Can you give me some proper answer?
     
  2. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    When structure is introduced in C, that time there is no concept of Object. So C standard commette(C99) decided to keep zero size of empty structure.

    In C++, Size of empty structure/class will be one byte bcoz.

    struct Test
    {
    public:
    void Disp( void );
    };

    int main()
    {
    Test tObj;
    tObj.Disp();
    return 0;
    }
    To call function atleast empty structure should have some size i.e. one byte. But there is no concept of member function in structure in C.
     

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