Memory allocated to an object of a class

C

C++fan

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
 
N

Nick Hounsome

C++fan said:
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.
 
V

Victor Bazarov

C++fan said:
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
 
P

pandy.song

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.
 
J

Jack Klein

On Tue, 6 Jan 2004 06:50:11 -0000, "Nick Hounsome"

[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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,836
Latest member
BuyBlissBitesCBD

Latest Threads

Top