Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Memory allocated to an object of a class

Reply
Thread Tools

Memory allocated to an object of a class

 
 
C++fan
Guest
Posts: n/a
 
      01-06-2004
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(varia ble_3)+sizeof(variable_4)+sizeof(variable_5)+sizeo f(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
 
Reply With Quote
 
 
 
 
Nick Hounsome
Guest
Posts: n/a
 
      01-06-2004

"C++fan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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(varia ble_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



 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-06-2004
"C++fan" <(E-Mail Removed)> 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(varia ble_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


 
Reply With Quote
 
pandy.song
Guest
Posts: n/a
 
      01-06-2004

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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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(varia ble_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



 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      01-07-2004
On Tue, 6 Jan 2004 06:50:11 -0000, "Nick Hounsome"
<(E-Mail Removed)> 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/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Object's allocated memory crb002@gmail.com Ruby 4 10-03-2007 07:51 AM
Heap allocated object's class constructor throws an exception. Martin C++ 1 11-15-2006 12:07 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Deleting memory allocated to the derived class Rohit C++ 7 03-01-2005 10:34 AM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM



Advertisments