sizeof(object) is different in ANSI and Unicode

Discussion in 'C++' started by Sunil Menon, Dec 1, 2003.

  1. Sunil Menon

    Sunil Menon Guest

    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
     
    Sunil Menon, Dec 1, 2003
    #1
    1. Advertising

  2. Sunil Menon

    John Carson Guest

    "Sunil Menon" <> wrote in message
    news:
    > 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)
     
    John Carson, Dec 1, 2003
    #2
    1. Advertising

  3. Sunil Menon

    Sunil Menon Guest

    > 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
     
    Sunil Menon, Dec 3, 2003
    #3
  4. "Sunil Menon" <> 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.
     
    Victor Bazarov, Dec 3, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Vinokur
    Replies:
    7
    Views:
    499
    Clark S. Cox III
    Aug 14, 2006
  2. Replies:
    1
    Views:
    493
  3. Replies:
    11
    Views:
    1,075
    Keith Thompson
    Apr 28, 2008
  4. Frank Iannarilli

    pre-ansi to ansi c++ conversion?

    Frank Iannarilli, Jul 21, 2009, in forum: C++
    Replies:
    2
    Views:
    416
  5. Nikolai Weibull

    sizeof(Class)/sizeof(Object)

    Nikolai Weibull, Dec 31, 2004, in forum: Ruby
    Replies:
    2
    Views:
    151
    Robert Klemme
    Dec 31, 2004
Loading...

Share This Page