I have the following situation compiling using Visual Studio 2003:

#include "stdafx.h"
#include <iostream>
#include <limits>

namespace Outer{
class Xyz {
public:

typedef struct {
enum { classConstant1 = 0x0001,
classConstant2 = 0x0002,
ForceSize2Bytes = 0x7FFF
}; // end enum
} Constants; // end struct

}; // end class Xyz

} // end namespace Outer


int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "The sizeof() Outer::Xyz::Constants = "
<< sizeof(Outer::Xyz::Constants)
<< std::endl;
std::cout << "Outer::Xyz::Constants::ForceSize2Bytes = 0x"
<< std::hex
<< Outer::Xyz::Constants::ForceSize2Bytes
<< std::endl;

std::cin.ignore((std::numeric_limits<std::streamsize>::max()),
'\n');

return 0;
} // end main()


Here's the output:

The sizeof() Outer::Xyz::Constants = 1
Outer::Xyz::Constants::ForceSize2Bytes = 0x7fff

The struct size is reported as one while the cosntants clearly output
a 2 byte value.

Does anyone knwo what's going one?

Thanks, HJS

Re: sizeof() struct w/ nested enum returns wrong size by Ulrich

Ulrich
Wed Apr 16 10:04:20 CDT 2008

homerjsimpson742@live.com wrote:
> typedef struct {
> enum { classConstant1 = 0x0001,
> classConstant2 = 0x0002,
> ForceSize2Bytes = 0x7FFF
> }; // end enum
> } Constants; // end struct

Note up front: stop this 'typedef struct ... name;'. In C++, that is
unnecessary (class, union, enum and struct all introduce new types).

Now, struct Constants is in fact empty, but due to requirements of the C++
standard, it actually has the minimal size 1. The anonymous enum nested in
Constants is just that, a type, and does not contribute to the containing
class' size. Instances of Constants also don't have any instance of the
enumeration.

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Michael Wöhrmann, Amtsgericht Hamburg HR B62 932

Re: sizeof() struct w/ nested enum returns wrong size by Bo

Bo
Wed Apr 16 10:17:33 CDT 2008

homerjsimpson742@live.com wrote:
> I have the following situation compiling using Visual Studio 2003:
>
> #include "stdafx.h"
> #include <iostream>
> #include <limits>
>
> namespace Outer{
> class Xyz {
> public:
>
> typedef struct {
> enum { classConstant1 = 0x0001,
> classConstant2 = 0x0002,
> ForceSize2Bytes = 0x7FFF
> }; // end enum
> } Constants; // end struct
>
> }; // end class Xyz
>
> } // end namespace Outer
>
>
> int _tmain(int argc, _TCHAR* argv[])
> {
> std::cout << "The sizeof() Outer::Xyz::Constants = "
> << sizeof(Outer::Xyz::Constants)
> << std::endl;
> std::cout << "Outer::Xyz::Constants::ForceSize2Bytes = 0x"
> << std::hex
> << Outer::Xyz::Constants::ForceSize2Bytes
> << std::endl;
>
> std::cin.ignore((std::numeric_limits<std::streamsize>::max()),
> '\n');
>
> return 0;
> } // end main()
>
>
> Here's the output:
>
> The sizeof() Outer::Xyz::Constants = 1
> Outer::Xyz::Constants::ForceSize2Bytes = 0x7fff
>
> The struct size is reported as one while the cosntants clearly
> output a 2 byte value.
>
> Does anyone knwo what's going one?

Yes, the struct doesn't contain any enum variables, it is just a
definition of the possible values.


Bo Persson



Re: sizeof() struct w/ nested enum returns wrong size by homerjsimpson742

homerjsimpson742
Wed Apr 16 10:36:46 CDT 2008

On Apr 16, 10:04=A0am, Ulrich Eckhardt <eckha...@satorlaser.com> wrote:
> homerjsimpson...@live.com wrote:
> > typedef struct {
> > =A0 =A0enum { =A0classConstant1 =3D 0x0001,
> > =A0 =A0 =A0 =A0 =A0 =A0classConstant2 =3D 0x0002,
> > =A0 =A0 =A0 =A0 =A0 =A0ForceSize2Bytes =3D 0x7FFF
> > =A0 =A0 =A0}; // end enum
> > } Constants; // end struct
>
> Note up front: stop this 'typedef struct ... name;'. In C++, that is
> unnecessary (class, union, enum and struct all introduce new types).
>
> Now, struct Constants is in fact empty, but due to requirements of the C++=

> standard, it actually has the minimal size 1. The anonymous enum nested in=

> Constants is just that, a type, and does not contribute to the containing
> class' size. Instances of Constants also don't have any instance of the
> enumeration.
>
> Uli
>
> --
> C++ FAQ:http://parashift.com/c++-faq-lite
>
> Sator Laser GmbH
> Gesch=E4ftsf=FChrer: Michael W=F6hrmann, Amtsgericht Hamburg HR B62 932

Thank you for taking the time to answer my question. It's a great
explanation; it makes perfect sense now.

Re: sizeof() struct w/ nested enum returns wrong size by Ben

Ben
Wed Apr 16 10:58:12 CDT 2008

homerjsimpson742@live.com wrote:
> I have the following situation compiling using Visual Studio 2003:
>
> #include "stdafx.h"
> #include <iostream>
> #include <limits>
>
> namespace Outer{
> class Xyz {
> public:
>
> typedef struct {
> enum { classConstant1 = 0x0001,
> classConstant2 = 0x0002,
> ForceSize2Bytes = 0x7FFF
> }; // end enum
> } Constants; // end struct
>
> }; // end class Xyz
>
> } // end namespace Outer
>
>
> int _tmain(int argc, _TCHAR* argv[])
> {
> std::cout << "The sizeof() Outer::Xyz::Constants = "
> << sizeof(Outer::Xyz::Constants)
> << std::endl;
> std::cout << "Outer::Xyz::Constants::ForceSize2Bytes = 0x"
> << std::hex
> << Outer::Xyz::Constants::ForceSize2Bytes
> << std::endl;
>
> std::cin.ignore((std::numeric_limits<std::streamsize>::max()),
> '\n');
>
> return 0;
> } // end main()
>
>
> Here's the output:
>
> The sizeof() Outer::Xyz::Constants = 1
> Outer::Xyz::Constants::ForceSize2Bytes = 0x7fff
>
> The struct size is reported as one while the cosntants clearly output
> a 2 byte value.
>
> Does anyone knwo what's going one?


sizeof(Outer::Xyz::Constants) is the size of an *instance* of type
Constants, not the size of the static data.

>
> Thanks, HJS