473,287 Members | 1,580 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,287 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 3514
"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...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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: 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.