473,725 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(abnormit y);

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 2462

"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(abnormit y);

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(abnormit y);

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
4012
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 answer. The problem is this: when the user presses the Calculate button, the whole page is reloaded and, on a large page, this is very noticeable. Is there any way that I can get the calculation done and the result displayed without reloading...
3
1816
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 abstract parent with four basic functions: Open, Read, Write, and Close. // Note alot of the stuff has been left out // just to give a basic picture class IO { public:
1
1368
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
1908
by: ad | last post by:
I want to develop DataBase application. How about Data Access Application Block? Is it useful?
9
8442
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
1470
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 the class use it ? Kinda like this. def some_function(var1, var2): do something with var1, var2 return result
28
6747
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 in general be done with one argument functions? Why would one want to do it anyway, other than showing that one knows and to save two characters?
4
1900
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 created? if ( answer == false ) { Would the idea of an abstract class simply be used to enforce integrity of the classes by disallowing the abstract class to be instantiated, or are there other purposes for it? }
5
1459
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 function' in the subclass. It works. Then i removed them in both classes, like here below: Public Class ford .....
2
14340
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 for $2;
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9116
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.