By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,933 Members | 1,574 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,933 IT Pros & Developers. It's quick & easy.

About the result of sizeof(a class without data members and virtual functions)

P: n/a
Hi, all.

What will we get from sizeof(a class without data members and virtual
functions)?

For example:

class abnormity {
public:
string name() { return "abnormity"; }
};

int size = sizeof(abnormity);

As far as I know, it could NOT be 0 (zero). Now I get 1 undering g++.

Does C++ standard specify the result of this case? Or is it
implementation-defined or undefined?

Best Regards,

Xiangliang Meng
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Xiangliang Meng" <xi*************@hotmail.com> wrote in message
news:cb**********@zcars0v6.ca.nortel.com...
Hi, all.

What will we get from sizeof(a class without data members and virtual
functions)?

For example:

class abnormity {
public:
string name() { return "abnormity"; }
};

int size = sizeof(abnormity);

As far as I know, it could NOT be 0 (zero). Now I get 1 undering g++.

Does C++ standard specify the result of this case? Or is it
implementation-defined or undefined?


The standard says that it cannot be zero. It does not say that it has to be
one, so it's implementation defined.

john
Jul 22 '05 #2

P: n/a
Xiangliang Meng wrote:
Hi, all.

What will we get from sizeof(a class without data members and virtual
functions)?

For example:

class abnormity {
public:
string name() { return "abnormity"; }
};

int size = sizeof(abnormity);

As far as I know, it could NOT be 0 (zero). Now I get 1 undering g++.

Does C++ standard specify the result of this case? Or is it
implementation-defined or undefined?

5.3.3 Sizeof

The sizeof operator yields the number of bytes in the object
representation of its operand. The operand is either an expression,
which is not evaluated, or a parenthesized typeid.

The sizeof operator shall not be applied to an expression that has
function or incomplete type, or to an enumeration type before all its
enumerators have been declared, or to the parenthesized name of such
types, or to an lvalue that designates a bitfield.

sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1; the
result of sizeof applied to any other fundamental type (3.9.1) is
implementation-defined.

[Note: in particular, sizeof(bool) and sizeof(wchar_t) are
implementation-defined. 69) ] [Note: See 1.7 for the definition of byte
and 3.9 for the definition of object representation. ]

When applied to a reference or a reference type, the result is the size
of the referenced type. When applied to a class, the result is the
number of bytes in an object of that class including any padding
required for placing objects of that type in an array. The size of a
most derived class shall be greater than zero (1.8).

The result of applying sizeof to a base class subobject is the size of
the base class type.70) When applied to an array, the result is the
total number of bytes in the array. This implies that the size of an
array of n elements is n times the size of an element.

The sizeof operator can be applied to a pointer to a function, but shall
not be applied directly to a function.

The lvalue-to-rvalue (4.1), array-to-pointer (4.2), and
function-to-pointer (4.3) standard conversions are not applied to the
operand of sizeof.

Types shall not be defined in a sizeof expression.

The result is a constant of type size_t. [Note: size_t is defined in the
standard header <cstddef>(18.1). ]
__________________
69) sizeof(bool) is not required to be 1.
70) The actual size of a base class subobject may be less than the
result of applying sizeof to the subobject, due to virtual base
classes and less strict padding requirements on base class subobjects.


Regards,

Ioannis Vranos
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.