Size of an Empty Class ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Size of an Empty Class ?

  1. #1
    Join Date
    Jun 2005
    Posts
    44

    Size of an Empty Class ?

    Hi All,

    what will b th esize of an empty class ?

    i tried and it shows 4.

    how is this ? any explanations ?

    Thanks in advance.

  2. #2
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,716

    Lightbulb Re: Size of an Empty Class ?

    when you calculate the size of a empty class.
    that time you can c it takes 1 byte.which is the size of a char
    which a compiler takes for storing the address of a empty class.

  3. #3
    Join Date
    May 2004
    Posts
    81

    Re: Size of an Empty Class ?

    Size of an empty class is OS or Compiler specific.

    If it is in dos/windows 64bit environment, the size of an empty class
    will be 1, bcoz the sizeof() operator can return a least of 1 byte, It can
    never return 0 and It's not bocz an empty class will occupy 1 byte of memory, In some OS it may be 2 or 4. Actually an empty class will not
    occupy any memory bcoz it won't contain any member's.

    Quote Originally Posted by humptydumpty
    when you calculate the size of a empty class.
    that time you can c it takes 1 byte.which is the size of a char
    which a compiler takes for storing the address of a empty class.
    For ur INFO a size of an empty class is not an address of a class where it
    sotres.

    Correct me, if i am wrong...

    Regards
    Sudhakar.M
    Verizon Data Services India Ltd.
    Last edited by sudhakarm; August 19th, 2005 at 01:40 AM.

  4. #4
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,716

    Lightbulb Re: Size of an Empty Class ?

    Quote Originally Posted by sudhakarm
    Size of an empty class is OS or Compiler specific.

    If it is in dos/windows 64bit environment, the size of an empty class
    will be 1, bcoz the sizeof() operator can return a least of 1 byte, It can
    never return 0 and It's not bocz an empty class will occupy 1 byte of memory, In some OS it may be 2 or 4. Actually an empty class will not
    occupy any memory bcoz it won't contain any member's.
    For ur INFO a size of an empty class is not an address of a class where it
    sotres.

    Correct me, if i am wrong...

    Regards
    Sudhakar.M
    Verizon Data Services India Ltd.
    you are abs right sud.size of an Empty Class is OS specific.
    first thing don't use OS or COMPILER
    it's OS Only but at the time when we tell anyone we always go for One Upper Level and can say that it's depend on Compiler.

    Some compiler take 2 byte for int
    some 4 byte etc

    Now in case of a EmptyClass
    However, even though there are no zero-size types in C++, C++ treat them One but C Treat Them 0 .and it takes 1 byte in memory to store the address of a class and better to say what is the range specified for the compiler for char it takes that much space to store the address of a class.

    There are still one or two C++ compilers that generate C code as their "assembly" language. To handle an empty class, they will generate a dummy member, so, for example:

    class A {};

    becomes:

    struct A {
    char __dummy;
    };
    Last edited by humptydumpty; August 19th, 2005 at 01:56 AM.

  5. #5
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: Size of an Empty Class ?

    According to Stroustrup: Why is the size of an empty class not zero?. Some more details can be found here
    Last edited by Ejaz; August 19th, 2005 at 02:01 AM.

  6. #6
    Join Date
    Jul 2005
    Location
    Germany
    Posts
    1,194

    Re: Size of an Empty Class ?

    I think you can change the size using #pragma pack(x) statement
    Please don't forget to rate users who helped you!

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Size of an Empty Class ?

    Quote Originally Posted by philkr
    I think you can change the size using #pragma pack(x) statement
    Notice that #pragma pack is a Microsoft specific directive.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center