Topic: sizeof question


Author: trujillom@lincom.com
Date: 1998/09/10
Raw View
What should be the sizeof a class considering that the class does not have
any data members(only methods). I know that if I have virtual functions, the
sizeof returns the size of the vptr, but what if I don't have these either?

A simple test using VC++ returns 1 from the sizeof, what is this the size of?

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: AllanW@my-dejanews.com
Date: 1998/09/11
Raw View
In article <6t99t2$ocb$1@nnrp1.dejanews.com>,
  trujillom@lincom.com wrote:
> What should be the sizeof a class considering that the class does not have
> any data members(only methods). I know that if I have virtual functions, the
> sizeof returns the size of the vptr, but what if I don't have these either?
>
> A simple test using VC++ returns 1 from the sizeof, what is this the size of?

All data types are required to have a size of at least one byte.
But don't rely on "sizeof" math:
    class A {}; // Size is 1
    class B : public A { char x[3]; }; // Size might not be 4
Some systems will be "smart" enough to make sizeof(B)==3, despite being
derived from class A.

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "David Sachs" <sachs@fnal.fnal.gov>
Date: 1998/09/12
Raw View
trujillom@lincom.com wrote in message <6t99t2$ocb$1@nnrp1.dejanews.com>...
>What should be the sizeof a class considering that the class does not have
>any data members(only methods). I know that if I have virtual functions,
the
>sizeof returns the size of the vptr, but what if I don't have these either?
>
>A simple test using VC++ returns 1 from the sizeof, what is this the size
of?

1 is the size of the smallest possible class object. The size is non-zero so
that arrays of the class are meaningful with each element having a different
address.

--
     The Klingon's favorite food was named by the first earthling to see it
      David Sachs - Fermilab - MS369 - PO Box 500 - Batavia, IL 60510
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1998/09/12
Raw View
trujillom@lincom.com wrote:
>
> What should be the sizeof a class considering that the class does not have
> any data members(only methods). I know that if I have virtual functions, the
> sizeof returns the size of the vptr, but what if I don't have these either?
>
> A simple test using VC++ returns 1 from the sizeof, what is this the size of?

Each time you create an object of that type, it must be given a unique
address. Since the smallest difference between two non-equal addresses
is 1, even an empty object uses up at least 1 byte of space.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/09/12
Raw View
<trujillom@lincom.com> wrote:
>What should be the sizeof a class considering that the class does not have
>any data members(only methods). I know that if I have virtual functions, the
>sizeof returns the size of the vptr, but what if I don't have these either?

For an exploration of this question, see

  http://www.cantrip.org/emptyopt.html

--
Nathan Myers
ncm@nospam.cantrip.org  http://www.cantrip.org/
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Vahur Krouverk <vahur@fv.aetec.ee>
Date: 1998/09/12
Raw View
trujillom@lincom.com wrote:

> What should be the sizeof a class considering that the class does not have
> any data members(only methods). I know that if I have virtual functions, the
> sizeof returns the size of the vptr, but what if I don't have these either?
>
> A simple test using VC++ returns 1 from the sizeof, what is this the size of?

Size of object, of course ;) Standard says, that objects of empty class shall
have nonzero size. There is good reason for this: to avoid aggregation members
aliasing. If size is zero, then pointers to 2 sequential empty subobjects would
be same.
See Nathan Myers article in DDJ (Dr. Dobb's Journal) #268, August 1997 for
discussion about empty members and their optimization.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]