473,388 Members | 1,557 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,388 software developers and data experts.

sizeof(object) is different in ANSI and Unicode

Dear All,
A class having no member variables and only a method sizeof(object)
will return 1byte in ANSI and two bytes in Unicode.
I have the answer for this of how in works in ANSI. But I don't
know it returns two bytes in UniCode.
Please help...

For ANSI:
In ISO/ANSI C++ Standard, 5.3.3 § 1, it stays: "The sizeof operator
yields the number of bytes in the object representation of its
operand.(...)
the result of sizeof applied to any other fundamental type
(_basic.fundamental_) is implementation-defined."
[Note: in particular, sizeof(bool) and sizeof(wchar_t) are
implementation-defined. ..."sizeof(bool) is not required to be 1."
The value of sizeof(bool) can be anything between 1 and N, where N is
positive integer number (I suppose 0 is not a reasonable value). One
can
read this as "size of bool type can not be smaller than size of char
type, as sizeof(char) is guaranteed to be 1"]
A class having no member variables and only a method sizeof(object)
will return 1byte.
Reason:
The basic issue is addressability. So, as long as memory's smallest
unit is char (sizeof(char)==1 by definition); no addressable object
can use less storage, even if it only uses up a single bit. Member
functions don't add to the sizeof a class.
All objects must have sizeof of at least one. This is so that you can
form a pointer to an empty object that is distinct from a pointer to
another empty object.
Examples:
Suppose sizeof(char )==1 // always true
Suppose sizeof(int )==4
Suppose sizeof(void*)==4 // possibly size of virtual pointer

For a class with virtual function, size of a virtual function is a
size of pointer to function.
the common implementation has only one virtual pointer in each object.
This virtual pointer points to a virtual table. There is one virtual
table for the whole class. Think of the virtual table as static data.
The virtual table has N entries if there are N virtual funcs. Eg,

struct Thing
{
virtual ~Thing();
virtual void f() const;
void g();
virtual void h() const;

int i;
};

Assuming sizeof(int)==4 and sizeof(any pointer)==4, then
sizeof(Thing)==sizeof(Thing::vptr)+sizeof(Thing::i )==8.

But there is a virtual table containing 3 entries. The compiler
generates this table internally. A class has a virtual table only if
it has a virtual function. For nonvirtual functions, an object doesn't
have to keep reference to it. I think they're just a piece of code
that compiler resolve at compile time.

Thanks & Regards
Sunil
Jul 22 '05 #1
3 3525
"Sunil Menon" <su***@itb-india.com> wrote in message
news:de*************************@posting.google.co m
Dear All,
A class having no member variables and only a method sizeof(object)
will return 1byte in ANSI and two bytes in Unicode.
Really? My quick test using VC++.Net 2002 gives a sizeof value of 1, not 2,
under Unicode.

I have the answer for this of how in works in ANSI. But I don't
know it returns two bytes in UniCode.
Please help...

For ANSI:
In ISO/ANSI C++ Standard, 5.3.3 § 1, it stays: "The sizeof operator
yields the number of bytes in the object representation of its
operand.(...)
the result of sizeof applied to any other fundamental type
(_basic.fundamental_) is implementation-defined."
[Note: in particular, sizeof(bool) and sizeof(wchar_t) are
implementation-defined. ..."sizeof(bool) is not required to be 1."


The quotation from the standard ends here. All the rest appears to be from
you or some other source. The standard says (section 1.8).

"a most derived object shall have a non-zero size and shall occupy one or
more bytes of storage"

Two bytes under Unicode would seem to satisfy this requirement. If there is
a compiler that yields a sizeof value of 2, then presumably it just pads the
class object for alignment and hence efficiency reasons.

--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #2
> Really? My quick test using VC++.Net 2002 gives a sizeof value of 1, not 2,
under Unicode.

Is VC++ .Net 2002 UniCode by default? Or is there some configuration u need to do?
Wot abt VC++ .Net 1.1?

I have both versions and I would like to try this too...:-)

Thanks & Regards
Sunil
Jul 22 '05 #3
"Sunil Menon" <su***@itb-india.com> wrote...
Really? My quick test using VC++.Net 2002 gives a sizeof value of 1, not 2, under Unicode. Is VC++ .Net 2002 UniCode by default? Or is there some configuration u

need to do? Wot abt VC++ .Net 1.1?

I have both versions and I would like to try this too...:-)


Please ask about it in microsoft.public.vc.ide_general newsgroup.
VC++ configuration is OT here.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Radde | last post by:
Hi all, class A { }; int main() { A a; cout<<"size "<<sizeof(a)<<endl;
9
by: M Welinder | last post by:
This doesn't work with any C compiler that I can find. They all report a syntax error: printf ("%d\n", (int)sizeof (char)(char)2); Now the question is "why?" "sizeof" and "(char)" have...
42
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
3
by: Raghu | last post by:
Is there some thing like sizeof operator that would give me the size of the object? Thanks.
5
by: junky_fellow | last post by:
Hi, I discussed about this earlier as well but I never got any satisfactory answer. So, I am initiating this again. Page 84, WG14/N869 "If both the pointer operand and the result point to...
40
by: Boltar | last post by:
Hi Why - using gcc on linux - does this return 0 in C but returns 1 in C+ +? I don't get it. #include <stdio.h> struct foo { };
0
by: swts | last post by:
hi, the following marshaling code gives me an errror of "type packet cannot be marshaled as an unmanaged structure; no meaningful size or offset can be managed". public static byte...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.