Connecting Tech Pros Worldwide Forums | Help | Site Map

Object Creation

Naren
Guest
 
Posts: n/a
#1: Jul 22 '05
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.

Karthik
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Object Creation


Naren wrote:
[color=blue]
> 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.[/color]
Check out any beginner's book on C++ and all your queries would be
answered in the first three chapters.

--
Karthik
Humans please 'removeme_' for my real email.
David Harmon
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Object Creation


On 27 Apr 2004 21:38:59 -0700 in comp.lang.c++,
narendranath.thadkal@honeywell.com (Naren) wrote,[color=blue]
>When constrcutor is called , Is space allocated for the object alraedy
>and only intialisation to object members done.[/color]

Yes, space is already allocated. It is the purpose of the constructor
to initialize that space into a working object.
[color=blue]
>What if the object is create using new?[/color]

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/

JKop
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Object Creation


Naren posted:
[color=blue]
> 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.[/color]


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


Sharad Kala
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Object Creation



"JKop" <NULL@NULL.NULL> wrote in message
news:lxKjc.5873$qP2.14002@news.indigo.ie...[color=blue]
> Naren posted:
>[/color]
[snip][color=blue]
> 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[/color]
[color=blue]
> char t; //Let's say char is 8-bit = 1 byte[/color]

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.
[color=blue]
> Hello(void)
> {
> j = 5;
> p = 43.4;
> t = 's';
> }
>
> double GiveMeSecretNumber(void)
> {
> return j + p;
> }
>
> };[/color]

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-lit....html#faq-29.4

[snip][color=blue][color=green]
>>[/color]
> 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.[/color]

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


jeffc
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Object Creation



"Naren" <narendranath.thadkal@honeywell.com> wrote in message
news:430c027f.0404272038.176a8d01@posting.google.c om...[color=blue]
> 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?[/color]

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.


JKop
Guest
 
Posts: n/a
#7: Jul 22 '05

re: Object Creation


[color=blue]
> 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[/color]


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
Rolf Magnus
Guest
 
Posts: n/a
#8: Jul 22 '05

re: Object Creation


JKop wrote:
[color=blue]
>[color=green]
>> 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[/color]
>
>
> Why would there be padding? Would some of the varibles have to be
> aligned somehow to special addresses in memory?[/color]

Yes, probably.
[color=blue]
> I don't see _why_ they would have to be.[/color]

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.
[color=blue]
> 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[/color]
[color=blue]
>*****char*t;***********//Let's*say*char*is*8-bit*=*1*byte[/color]

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.

Closed Thread