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