473,653 Members | 2,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.fundame ntal_) 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::v ptr)+sizeof(Thi ng::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 3546
"Sunil Menon" <su***@itb-india.com> wrote in message
news:de******** *************** **@posting.goog le.com
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.fundame ntal_) 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.publi c.vc.ide_genera l 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
4169
by: Radde | last post by:
Hi all, class A { }; int main() { A a; cout<<"size "<<sizeof(a)<<endl;
9
3008
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 identical precedence and right-to-left parsing, so why isn't the above equivalent to printf ("%d\n", (int)sizeof ((char)(char)2));
42
2388
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
3
1335
by: Raghu | last post by:
Is there some thing like sizeof operator that would give me the size of the object? Thanks.
5
3678
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 elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the
40
2785
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
1140
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 StructureToBytesArray(object obj) { int len = Marshal.SizeOf(obj); byte arr = new byte; IntPtr ptr = Marshal.AllocHGlobal(len); Marshal.StructureToPtr(obj, ptr, true);
0
8370
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8811
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...
1
8470
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7302
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
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
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2707
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.