473,289 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

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

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
2 2436

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Aspersion | last post by:
I'm building an ASP page that has a lot of text and graphics. There is a calculation facility on the page. The user enters several numbers in a form and presses a button to see the calculated...
3
by: fernandez.dan | last post by:
I'm still learning how to use Object Oriented concepts. I'm have a basic question dealing with design. I have two classes that deal with I/O pertaining to network and usb that inherit from an...
1
by: kroty | last post by:
Hi, I want to learn about data structures in C. I know nothing about the subject. I just have read k&r and I feel confortable coding in C. :) Could you recommend me some book? Thanks!
4
by: ad | last post by:
I want to develop DataBase application. How about Data Access Application Block? Is it useful?
9
by: Sameer | last post by:
Hi, I have a class. class C1 { int i ; char c ; } ; let me say the size of int is 4 and char is 1. Then if I say sizeof(C1) it should be 5, why is it showing 8 ?
7
by: nephish | last post by:
lo there all, i have an app that runs three classes as threads in the same program. some of them need to be able to share some functions. Can i set a function before i define a class and have...
28
by: Howard Bryce | last post by:
I have come across code containing things like sizeof int How come that one can invoke sizeof without any parentheses surrounding its argument? Is this admissible within the standard? Can it...
4
by: Devon Null | last post by:
I have been exploring the concept of abstract classes and I was curious - If I do not define a base class as abstract, will it be instantiated (hope that is the right word) when a derived class is...
5
by: Ben | last post by:
Hi, i defined a function in the base class 'ford' and the same function (with different output) in subclass "peugeot". I first put 'Overridable function' in the base class and 'Overrides...
2
by: eros | last post by:
CREATE OR REPLACE FUNCTION public.f_customerlogininfo ( varchar, varchar, varchar ) RETURNS SETOF public.v_customerlogininfo AS' declare p_schm alias for $1; p_contact alias...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.