Object Creation

N

Naren

Hello All,
I am confused on the object creation.

Is it the constructor or something else?

When constrcutor is called , Is space allocated for the object alraedy
and only intialisation to object members done.

What if the object is create using new?

Could anyone give me a detailed explaination on this?

Thanks in advance.

Regards,
Naren.
 
K

Karthik

Naren said:
Hello All,
I am confused on the object creation.

Is it the constructor or something else?

When constrcutor is called , Is space allocated for the object alraedy
and only intialisation to object members done.

What if the object is create using new?

Could anyone give me a detailed explaination on this?

Thanks in advance.

Regards,
Naren.
Check out any beginner's book on C++ and all your queries would be
answered in the first three chapters.
 
D

David Harmon

On 27 Apr 2004 21:38:59 -0700 in comp.lang.c++,
(e-mail address removed) (Naren) wrote,
When constrcutor is called , Is space allocated for the object alraedy
and only intialisation to object members done.

Yes, space is already allocated. It is the purpose of the constructor
to initialize that space into a working object.
What if the object is create using new?

Same thing, space is already allocated.

Constructors are covered in section 10 of Marshall Cline's C++ FAQ. It
is always good to check the FAQ before posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
 
J

JKop

Naren posted:
Hello All,
I am confused on the object creation.

Is it the constructor or something else?

When constrcutor is called , Is space allocated for the object alraedy
and only intialisation to object members done.

What if the object is create using new?

Could anyone give me a detailed explaination on this?

Thanks in advance.

Regards,
Naren.


Please read this using a monospace font if possible.


class Hello
{
public:

int j; //Let's say int is 32-bit = 4 bytes
double p; //Let's say double is 64-Bit = 8 bytes
char t; //Let's say char is 8-bit = 1 byte

Hello(void)
{
j = 5;
p = 43.4;
t = 's';
}

double GiveMeSecretNumber(void)
{
return j + p;
}

};



There's the "Hello" class. It has three public member variables: j p t. It
has a contructor, "Hello(void)", and it has a public member funciton,
"GiveMeSecretNumber(void)".


Now lets make an object of that class:

int main(void)
{
int numbr;

Hello greeting;

return 0;
}



What you've done is declared a variable, very simply. Just as how I've
declared "int numbr". Our variable is of type "Hello" and it's name is
"greeting". When a variable's type is a class, we call it an object! The
first thing that happens is that memory is allocated for the member
variables, ie. j p t, totalling 13 bytes. Every time you make an object of
the class, eg.:

Hello gh;
Hello rs;
Hello nm;

Each object takes up 13 bytes in memory, no more, no less.

From there, the appropriate constructor is called.


Then the program moves on to the next line of code.


Now when you call one of the member functions, eg.:

greeting.GetSecretNumber();

All that happens is that the function "Hello::GetSecretNumber" is called,
and within that function, when it mentions j p t, it's talking about the
member variables of the object "greeting".
 
S

Sharad Kala

JKop said:
Naren posted:
[snip]
Please read this using a monospace font if possible.


class Hello
{
public:

int j; //Let's say int is 32-bit = 4 bytes
double p; //Let's say double is 64-Bit = 8 bytes
char t; //Let's say char is 8-bit = 1 byte

I suppose you don't mean to say 1 byte is 8 bits.
C++ standard ensures that a byte is _at least_ 8 bits, could be more.
Hello(void)
{
j = 5;
p = 43.4;
t = 's';
}

double GiveMeSecretNumber(void)
{
return j + p;
}

};

In C++ you don't need to type void if a function takes no parameters.
Look at thos FAQ - http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.4

[snip]
What you've done is declared a variable, very simply. Just as how I've
declared "int numbr". Our variable is of type "Hello" and it's name is
"greeting". When a variable's type is a class, we call it an object! The
first thing that happens is that memory is allocated for the member
variables, ie. j p t, totalling 13 bytes.

How can it be 13 bytes always? Can't there be padding between the fields?
I could very well expect sizeof(Hello) to be 24 bytes.

-Sharad
 
J

jeffc

Naren said:
Hello All,
I am confused on the object creation.

Is it the constructor or something else?

When constrcutor is called , Is space allocated for the object alraedy
and only intialisation to object members done.

What if the object is create using new?

Could anyone give me a detailed explaination on this?

It's still not clear to me what you need to know. When you call "new" or
use automatic allocation, storage is allocated at that point, and then the
constructor is called. Is the specific order of things important to you for
some reason? Abstractly, you should view it as all happening at the same
time. But obviously you cannot initialize a variable if space has not been
allocated for that variable.
 
J

JKop

How can it be 13 bytes always? Can't there be padding between the
fields? I could very well expect sizeof(Hello) to be 24 bytes.

-Sharad


Why would there be padding? Would some of the varibles have to be aligned
somehow to special addresses in memory? I don't see _why_ they would have to
be.

-JKop
 
R

Rolf Magnus

JKop said:
Why would there be padding? Would some of the varibles have to be
aligned somehow to special addresses in memory?

Yes, probably.
I don't see _why_ they would have to be.

There are systems where a 32bit variable would have to be aligned on a
32bit boundary, on some even a 64bit variable (which double might be)
has to be aligned to a 64 bit boundary. On those systems that don't
require such alignment, badly aligned variables are often still a lot
slower.
class Hello
{
public:

     int j;            //Let's say int is 32-bit = 4 bytes
     double p;         //Let's say double is 64-Bit = 8 bytes
     char t;           //Let's say char is 8-bit = 1 byte

Now, if you consider the machine that was assumed in that example and
that requires 64bit alignment, that would mean that there have to be 4
alignment bytes between j and p. Since the struct must also be usable
in arrays, it also needs to have another 7 alignment bytes after t.
So that means we have 24 bytes for the struct.
On my system, that struct happens to be 16 bytes big. I guess it adds 3
empty bytes after t to get a 32bit alginment.
 

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
474,167
Messages
2,570,910
Members
47,453
Latest member
MadelinePh

Latest Threads

Top