472,784 Members | 962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 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 2390

"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...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.