object question

Discussion in 'C++' started by junw2000@gmail.com, Jul 18, 2006.

  1. Guest

    how much memory is allocated when creating an object of an empty class?

    Thanks.

    Jack
     
    , Jul 18, 2006
    #1
    1. Advertising

  2. AB Guest

    > how much memory is allocated when creating an object of an empty class?

    Try this...

    class Empty
    {
    } ;

    Empty e ;

    cout<<"\nSizeof "<<typeid(e).name()<<" = "<<sizeof(Empty) ;

    Output is 1 when using MSVC 8.0
     
    AB, Jul 18, 2006
    #2
    1. Advertising

  3. Guest

    wrote:
    > how much memory is allocated when creating an object of an empty class?
    >
    > Thanks.


    I imaging that this would be a good way of getting the size of a
    class/instance of a class -

    class DoNothing
    {
    public:
    DoNothing() {}
    //int a;
    };

    int main()
    {
    DoNothing donothing;
    std::cout << sizeof( donothing ) << std::endl;
    std::cout << sizeof( DoNothing ) << std::endl;
    }
     
    , Jul 18, 2006
    #3
  4. * :
    > how much memory is allocated when creating an object of an empty class?
    >


    Depends on the compiler and the context.

    As a base class object it can occupy zero bytes, otherwise more.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jul 18, 2006
    #4
  5. Guest

    Alf P. Steinbach wrote:
    > * :
    > > how much memory is allocated when creating an object of an empty class?
    > >

    >

    It purely depends upon the compiler. But in most of the compilers it
    will be 1 byte(Size of char). Emplty class object takes the minimum
    space to identify it as an entity in memory.

    If it will not take any space the in the below example
    Class A{
    };

    A a, b;

    &a and &b can be same. Which is pratically impossible(two
    differentities can't reside in same place)
     
    , Jul 18, 2006
    #5
  6. Guest

    wrote:
    > Alf P. Steinbach wrote:
    > > * :
    > > > how much memory is allocated when creating an object of an empty class?
    > > >

    > >

    > It purely depends upon the compiler. But in most of the compilers it
    > will be 1 byte(Size of char). Emplty class object takes the minimum
    > space to identify it as an entity in memory.
    >
    > If it will not take any space the in the below example
    > Class A{
    > };
    >
    > A a, b;
    >


    Here you are not talking about size of a and b, &a and &b means
    "address of".
    &a and &b will not be same in above case, but this is nothing to do
    with size
    Size of a and b will be same but they will have different address. But
    yes, size depends upon compliers and usually 1 byte.

    > &a and &b can be same. Which is pratically impossible(two
    > differentities can't reside in same place)
     
    , Jul 18, 2006
    #6
  7. wrote:

    > wrote:


    >> Class A{
    >> };
    >>
    >> A a, b;


    >> &a and &b can be same. Which is pratically impossible(two
    >> differentities can't reside in same place)


    > Here you are not talking about size of a and b, &a and &b means
    > "address of".
    > &a and &b will not be same in above case, but this is nothing to do
    > with size
    > Size of a and b will be same but they will have different address. But
    > yes, size depends upon compliers and usually 1 byte.


    You misunderstood him. If 'a' and 'b' *would* have a size of 0 *and*
    they are allocated right next to each other, so that a + sizeof(a) == b,
    then &a == &b, because sizeof(a) == 0. Since &a == &b is invalid for
    different objects, sizeof(a) cannot be 0 in this case.

    So in the above example, &a and &b (address of 'a' and address of
    'b') could be the same, if sizeof(a) could be 0, which is what tarun
    said.

    regards
    --
    jb

    (reply address in rot13, unscramble first)
     
    Jakob Bieling, Jul 18, 2006
    #7
  8. Guest

    Replying to Alf P. Steinbach


    I dont't think logically Object of size 0 is possible.

    Even for emptyclass it would required some min. byte (which is on most
    platform 1 byte,)


    --
    Raxit Sheth
     
    , Jul 18, 2006
    #8
  9. Sumit RAJAN Guest

    wrote:
    > Replying to Alf P. Steinbach


    Please quote what you are replying to.

    > I dont't think logically Object of size 0 is possible.
    >
    > Even for emptyclass it would required some min. byte (which is on most
    > platform 1 byte,)


    If you look carefully at Alf's reply, you will find that he says it
    *can* be zero in case of a *base* class. Otherwise, the only requirement
    is that it have a non-zero size.

    Google for "Empty Base Class Optimization" in this regard.
    --
    Sumit RAJAN <>
     
    Sumit RAJAN, Jul 18, 2006
    #9
  10. KMS Guest

    Hi,

    It depends on compiler and it will take 1 byte in most of the
    compilers.

    Reason:

    Class can be empty but there are possibilities that there can be an
    empty class with virtual function, in which case there will be a
    virtual table and it need a pointer to store address of virtual
    table.So we need a byte to store the address of virtual table.

    I remember that'z what even Bjarne Stroustroup said in his book C++
    Programming Language.

    Thanks
    Suresh K.M


    wrote:
    > how much memory is allocated when creating an object of an empty class?
    >
    > Thanks.
    >
    > Jack
     
    KMS, Jul 18, 2006
    #10
  11. Rolf Magnus Guest

    Jakob Bieling wrote:

    > wrote:
    >
    >> wrote:

    >
    >>> Class A{
    >>> };
    >>>
    >>> A a, b;

    >
    >>> &a and &b can be same. Which is pratically impossible(two
    >>> differentities can't reside in same place)

    >
    >> Here you are not talking about size of a and b, &a and &b means
    >> "address of".
    >> &a and &b will not be same in above case, but this is nothing to do
    >> with size
    >> Size of a and b will be same but they will have different address. But
    >> yes, size depends upon compliers and usually 1 byte.

    >
    > You misunderstood him. If 'a' and 'b' *would* have a size of 0 *and*
    > they are allocated right next to each other, so that a + sizeof(a) == b,
    > then &a == &b, because sizeof(a) == 0. Since &a == &b is invalid for
    > different objects, sizeof(a) cannot be 0 in this case.


    Nobody forces the compiler to put a and b directly next to each other. So
    that's not really a good example.

    The example of an array is better. In:

    A array[5];

    each array member must have a distinct address, but that's not possible if
    the members have a size of zero. All elements would be at the same address,
    or depending on your view, actually not be at any address at all. After
    all, an address marks some storage location, and an object with a size of
    zero does not occupy any storage and thus can't have a location there.
     
    Rolf Magnus, Jul 18, 2006
    #11
  12. * :
    > Alf P. Steinbach wrote:
    >> * :
    >>> how much memory is allocated when creating an object of an empty class?
    >>>

    > It purely depends upon the compiler.


    No, I'm sorry, that's incorrect.

    As I wrote (but you snipped all I wrote, don't do that) it depends on
    the compiler but also on the context.

    Quoting myself:

    <q>
    Depends on the compiler and the context.

    As a base class object it can occupy zero bytes, otherwise more.
    </q>


    > It purely depends upon the compiler. But in most of the compilers it
    > will be 1 byte(Size of char). Emplty class object takes the minimum
    > space to identify it as an entity in memory.
    >
    > If it will not take any space the in the below example
    > Class A{
    > };
    >
    > A a, b;
    >
    > &a and &b can be same. Which is pratically impossible(two
    > differentities can't reside in same place)


    Right.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jul 18, 2006
    #12
  13. Ron Natalie Guest

    wrote:
    > how much memory is allocated when creating an object of an empty class?
    >

    All objects must occupy at least one byte.
    Your compiler is free to generate anything that size or
    greater.
     
    Ron Natalie, Jul 18, 2006
    #13
  14. Ron Natalie Guest

    wrote:
    > wrote:
    >> Alf P. Steinbach wrote:
    >>> * :
    >>>> how much memory is allocated when creating an object of an empty class?
    >>>>

    >> It purely depends upon the compiler. But in most of the compilers it
    >> will be 1 byte(Size of char). Emplty class object takes the minimum
    >> space to identify it as an entity in memory.
    >>
    >> If it will not take any space the in the below example
    >> Class A{
    >> };
    >>
    >> A a, b;
    >>

    >
    > Here you are not talking about size of a and b, &a and &b means
    > "address of".
    > &a and &b will not be same in above case, but this is nothing to do
    > with size
    > Size of a and b will be same but they will have different address. But
    > yes, size depends upon compliers and usually 1 byte.
    >

    A better example would be
    A array[2];

    The size of an element in the array can't be zero or else
    the array indexing rules wouldn't work.
     
    Ron Natalie, Jul 18, 2006
    #14
  15. Frederick Gotham, Jul 18, 2006
    #15
  16. Rolf Magnus <> wrote:
    > Jakob Bieling wrote:
    >
    >> wrote:
    >>
    >>> wrote:

    >>
    >>>> Class A{
    >>>> };
    >>>>
    >>>> A a, b;

    >>
    >>>> &a and &b can be same. Which is pratically impossible(two
    >>>> differentities can't reside in same place)

    >>
    >>> Here you are not talking about size of a and b, &a and &b means
    >>> "address of".
    >>> &a and &b will not be same in above case, but this is nothing to do
    >>> with size
    >>> Size of a and b will be same but they will have different address.
    >>> But yes, size depends upon compliers and usually 1 byte.

    >>
    >> You misunderstood him. If 'a' and 'b' *would* have a size of 0
    >> *and* they are allocated right next to each other, so that a +
    >> sizeof(a) == b, then &a == &b, because sizeof(a) == 0. Since &a ==
    >> &b is invalid for different objects, sizeof(a) cannot be 0 in this
    >> case.

    >
    > Nobody forces the compiler to put a and b directly next to each
    > other. So that's not really a good example.


    Right, assume tarun was referring to the possibility of this event.
    I should have made clearer that the "*and* they are allocated right next
    to each other" also belongs to the "if this happens" part.

    regards
    --
    jb

    (reply address in rot13, unscramble first)
     
    Jakob Bieling, Jul 18, 2006
    #16
  17. Default User Guest

    wrote:

    > Replying to Alf P. Steinbach


    When you reply, use the standard quoting mechanism. Google now provides
    that by default. See the FAQ for other tips:
    <http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4>



    Brian
     
    Default User, Jul 18, 2006
    #17
  18. Default User Guest

    KMS wrote:

    > Hi,



    Please don't top-post. Your reply belongs following or interspersed
    with properly trimmed quotes. See the newsgroup FAQ:
    <http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4>




    Brian
     
    Default User, Jul 18, 2006
    #18
  19. Jakob Bieling <> wrote:

    > Right, assume tarun was referring to the possibility of this event.


    *I assumed
    --
    jb

    (reply address in rot13, unscramble first)
     
    Jakob Bieling, Jul 18, 2006
    #19
  20. Guest

    Frederick Gotham wrote:
    > posted:
    >
    > > how much memory is allocated when creating an object of an empty class?

    >
    >
    > Here's a link to Bjarne's answer:
    >
    >
    > http://www.research.att.com/~bs/bs_faq2.html#sizeof-empty
    >


    Thank you all.
    For the followinf code:

    #include <iostream>
    class A{

    };

    int main(){
    A a[5];
    std::cout<<"sizeof(a):"<<sizeof(a)<<std::endl;
    }

    THe output is:

    sizeof(a):5

    I use gcc.
     
    , Jul 18, 2006
    #20
    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. =?Utf-8?B?TWFubnkgQ2hvaGFu?=

    Question: Object Reference not set to an instance of an object.

    =?Utf-8?B?TWFubnkgQ2hvaGFu?=, Jan 3, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    2,223
    Kevin Spencer
    Jan 3, 2005
  2. jon wayne
    Replies:
    9
    Views:
    732
    Jim Langston
    Sep 22, 2005
  3. gg9h0st
    Replies:
    4
    Views:
    382
    Salt_Peter
    Oct 24, 2006
  4. DanielJohnson
    Replies:
    7
    Views:
    2,036
    James Kanze
    Jan 17, 2009
  5. Elf M. Sternberg
    Replies:
    15
    Views:
    281
    Matthias Reitinger
    Jul 29, 2009
Loading...

Share This Page