Memory allocated to an object of a class

Discussion in 'C++' started by C++fan, Jan 6, 2004.

  1. C++fan

    C++fan Guest

    Hi all:

    I have a question about memory allocation to an object of a class.
    For example, I define the following class:

    class example_class{

    public:
    example_class();
    void funtion_1();
    void function_2();
    int variable_1;
    double variable_2;

    protected:
    struct st {
    int variable_3;
    double variable_4;
    } s1;

    char *variable_5;

    private:
    int *variable_6;

    }

    Then create an object:

    example_class *object_1 = new example_class;

    My question is:

    1. How much memory space will be allocated to object_1? Is the
    following formula correct?
    sizeof(variable_1)+sizeof(variable_2)+sizeof(variable_3)+sizeof(variable_4)+sizeof(variable_5)+sizeof(variable_6)
    = sizeof(object_1)

    2. What is the entry address of object_1?
    object_1 = &(variable_1), i.e., the entry address of object_1 is the
    address of variable_1.
    Or object_1 = &(s1) = &(variable_3), i.e., the entry address of
    object_1 is the address of s1 or the address of variable_3.
    Which is correct? Or both are wrong?

    Thanks a lot.

    Jack
    C++fan, Jan 6, 2004
    #1
    1. Advertising

  2. "C++fan" <> wrote in message
    news:...
    > Hi all:
    >
    > I have a question about memory allocation to an object of a class.
    > For example, I define the following class:
    >
    > class example_class{
    >
    > public:
    > example_class();
    > void funtion_1();
    > void function_2();
    > int variable_1;
    > double variable_2;
    >
    > protected:
    > struct st {
    > int variable_3;
    > double variable_4;
    > } s1;
    >
    > char *variable_5;
    >
    > private:
    > int *variable_6;
    >
    > }
    >
    > Then create an object:
    >
    > example_class *object_1 = new example_class;
    >
    > My question is:
    >
    > 1. How much memory space will be allocated to object_1? Is the
    > following formula correct?
    >

    sizeof(variable_1)+sizeof(variable_2)+sizeof(variable_3)+sizeof(variable_4)+
    sizeof(variable_5)+sizeof(variable_6)
    > = sizeof(object_1)


    It's not gauranteed.

    Whether it is likely to be true depends largely on the alignment
    requirements of your implementation.
    For example - if your platform uses 4 byte ints then the size of the
    following is almost certainly 8 rather than 5:
    class X { char c; int i; }
    This is because it will need to add 3 bytes of padding to get i aligned.
    Sometimes size will depend on order of members e.g. struct X { char
    c1,c2,c3,c4; int i; } will be 8 but
    struct X { char c1; int i; char c2; } will be 12! Therefore you should
    always put members in descending order of size.

    Note that even reordering the first example to struct X { int i; char c; }
    it will still have size 8 because otherwise you couldn't have an array of
    them.

    Strictly - the compiler is allowed a lot of leeway to rearrange the
    members - I don't know the details off hand except that it can
    definitely rearrange if you use any type of access decl
    (public,private,protected) and probably if it is anything other than POD.

    NB If the class has virtual base classes or functions it will be at least
    sizeof(void*) bigger than you would expect.
    >
    > 2. What is the entry address of object_1?
    > object_1 = &(variable_1), i.e., the entry address of object_1 is the
    > address of variable_1.
    > Or object_1 = &(s1) = &(variable_3), i.e., the entry address of
    > object_1 is the address of s1 or the address of variable_3.
    > Which is correct? Or both are wrong?
    >


    It is definitely undefined in this case because of the
    public,protected,private.

    If you used a plain old struct without access decls it would be &variable_1
    on any implementation that anyone would buy whether the standard gaurantees
    it or not.

    > Thanks a lot.
    >
    > Jack
    Nick Hounsome, Jan 6, 2004
    #2
    1. Advertising

  3. "C++fan" <> wrote...
    > I have a question about memory allocation to an object of a class.
    > For example, I define the following class:
    >
    > class example_class{
    >
    > public:
    > example_class();
    > void funtion_1();
    > void function_2();
    > int variable_1;
    > double variable_2;
    >
    > protected:
    > struct st {
    > int variable_3;
    > double variable_4;
    > } s1;
    >
    > char *variable_5;
    >
    > private:
    > int *variable_6;
    >
    > }
    >
    > Then create an object:
    >
    > example_class *object_1 = new example_class;
    >
    > My question is:
    >
    > 1. How much memory space will be allocated to object_1? Is the
    > following formula correct?
    >

    sizeof(variable_1)+sizeof(variable_2)+sizeof(variable_3)+sizeof(variable_4)+
    sizeof(variable_5)+sizeof(variable_6)
    > = sizeof(object_1)


    Not necessarily. The object may have padding, the 's1' may have padding.

    > 2. What is the entry address of object_1?
    > object_1 = &(variable_1), i.e., the entry address of object_1 is the
    > address of variable_1.
    > Or object_1 = &(s1) = &(variable_3), i.e., the entry address of
    > object_1 is the address of s1 or the address of variable_3.
    > Which is correct? Or both are wrong?


    Since the 'example_class' is not a POD, there is no guarantee that its
    address coincides with the first member declared in it. Such address
    coincidence is only guaranteed for PODs (see 9.2/17).

    Victor
    Victor Bazarov, Jan 6, 2004
    #3
  4. C++fan

    pandy.song Guest

    The result depends.

    (1) Byte Allignment.
    (2) There are virtual table if there are virtual function.

    I think the entry address depends on the implementation of compiler.

    "C++fan" <> wrote in message
    news:...
    > Hi all:
    >
    > I have a question about memory allocation to an object of a class.
    > For example, I define the following class:
    >
    > class example_class{
    >
    > public:
    > example_class();
    > void funtion_1();
    > void function_2();
    > int variable_1;
    > double variable_2;
    >
    > protected:
    > struct st {
    > int variable_3;
    > double variable_4;
    > } s1;
    >
    > char *variable_5;
    >
    > private:
    > int *variable_6;
    >
    > }
    >
    > Then create an object:
    >
    > example_class *object_1 = new example_class;
    >
    > My question is:
    >
    > 1. How much memory space will be allocated to object_1? Is the
    > following formula correct?
    >

    sizeof(variable_1)+sizeof(variable_2)+sizeof(variable_3)+sizeof(variable_4)+
    sizeof(variable_5)+sizeof(variable_6)
    > = sizeof(object_1)
    >
    > 2. What is the entry address of object_1?
    > object_1 = &(variable_1), i.e., the entry address of object_1 is the
    > address of variable_1.
    > Or object_1 = &(s1) = &(variable_3), i.e., the entry address of
    > object_1 is the address of s1 or the address of variable_3.
    > Which is correct? Or both are wrong?
    >
    > Thanks a lot.
    >
    > Jack
    pandy.song, Jan 6, 2004
    #4
  5. C++fan

    Jack Klein Guest

    On Tue, 6 Jan 2004 06:50:11 -0000, "Nick Hounsome"
    <> wrote in comp.lang.c++:

    [snip]

    > If you used a plain old struct without access decls it would be &variable_1
    > on any implementation that anyone would buy whether the standard gaurantees
    > it or not.


    The standard does guarantee this for POD structures.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jan 7, 2004
    #5
    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. Duncan
    Replies:
    0
    Views:
    391
    Duncan
    Jul 21, 2003
  2. Replies:
    5
    Views:
    619
    Matt Wharton
    Dec 9, 2004
  3. Rohit
    Replies:
    7
    Views:
    400
    Prawit Chaivong
    Mar 1, 2005
  4. Martin
    Replies:
    1
    Views:
    450
    mlimber
    Nov 15, 2006
  5. Replies:
    4
    Views:
    100
    Robert Klemme
    Oct 3, 2007
Loading...

Share This Page