473,394 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Object Creation

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.
Jul 22 '05 #1
7 1961
Naren wrote:
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.

--
Karthik
Humans please 'removeme_' for my real email.
Jul 22 '05 #2
On 27 Apr 2004 21:38:59 -0700 in comp.lang.c++,
na******************@honeywell.com (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/

Jul 22 '05 #3
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".
Jul 22 '05 #4

"JKop" <NU**@NULL.NULL> wrote in message
news:lx******************@news.indigo.ie...
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-lit....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
Jul 22 '05 #5

"Naren" <na******************@honeywell.com> wrote in message
news:43**************************@posting.google.c om...
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.
Jul 22 '05 #6
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
Jul 22 '05 #7
JKop wrote:
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?


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.

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Richard | last post by:
Hi all, I am looking for some help on understanding the overhead associated with object creation in Java. I am writing an application where I have written a class to encapsulate some text...
54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
3
by: John Ratliff | last post by:
When I dereference a pointer, does it make a copy of the object? Say I had a singleton, and wanted an static method to retrieve it from the class. class foo { private: static foo *bar; ...
8
by: Anthony Munter | last post by:
I have a web application with impersonate=”true” in Web.config and on my own logon page I allow the user to either - specify a userid/password for the app to impersonate when calling legacy...
3
by: Nick Dreyer | last post by:
I was quite surprised to notice that Sub New() gets called twice, once at declaration time and once at creation time. I can't figure out why it would be called at declaration if there is no class...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
44
by: petermichaux | last post by:
Hi, I have been using the following line of code to create an object called "Serious" if it doesn't already exist. if (Serious == null) {var Serious = {};} This works in the scripts I use...
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
0
by: Dhananjay | last post by:
Hi All, I want to develop one application in vb.net for exchange 2000. I tried to add one contact with the code snippet below. The same logic is there for appointment on Microsoft's site. (I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.