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

X

Xiangliang Meng

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
 
J

John Harrison

Xiangliang Meng said:
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
 
I

Ioannis Vranos

Xiangliang said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top