| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Rahul" |
| Date: |
03 Feb 2008 07:33:13 AM |
| Object: |
creation of an object |
Hi Everyone,
I have the following code,
class Test
{
public : Test()
{
}
};
int main()
{
Test object;
return(0);
}
If my understanding is correct, the constructor isn't responsible for
the allocation of the memory for the object, so how does the actual
memory allocation occur?
What exactly happens beneath the following statement,
Test object;
Thanks in advance!!!
.
|
|
| User: "Scott McPhillips [MVP] org-dot-mvps-at-scottmcp" |
|
| Title: Re: creation of an object |
03 Feb 2008 09:48:26 AM |
|
|
"Rahul" <sam_cit@yahoo.co.in> wrote in message
news:8b7a8e50-dcf1-4c66-bd6b-3d9d2ecae2b2@b2g2000hsg.googlegroups.com...
int main()
{
Test object;
return(0);
}
If my understanding is correct, the constructor isn't responsible for
the allocation of the memory for the object, so how does the actual
memory allocation occur?
In almost all architectures the compiler generates code at the function
entry point to adjust the stack pointer register by the size of the
function's stack variables. It also undoes this adjustment at the
function's return. Both operations are incredibly efficient in machine
language.
--
Scott McPhillips [VC++ MVP]
.
|
|
|
|
| User: "James Kanze" |
|
| Title: Re: creation of an object |
03 Feb 2008 12:38:11 PM |
|
|
On Feb 3, 2:33 pm, Rahul <sam_...@yahoo.co.in> wrote:
I have the following code,
class Test
{
public : Test()
{
}
};
int main()
{
Test object;
return(0);
}
If my understanding is correct, the constructor isn't
responsible for the allocation of the memory for the object,
so how does the actual memory allocation occur?
Compiler's problem, not yours:-). The memory must be allocated
somehow.
What exactly happens beneath the following statement,
Test object;
Depends on the implementation. Typically, the compiler will sum
up the total amount of memory needed in the function, and
allocate it on the stack at the start of the function. All that
is guaranteed, however, is that the memory will be allocated
before the constructor is called, and that it won't be freed
until the destructor has been called (with a few special
exceptions in the latter case).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
|
|
|
|
| User: "Raymond" |
|
| Title: Re: creation of an object |
03 Feb 2008 10:03:34 AM |
|
|
Rahul wrote:
Hi Everyone,
I have the following code,
class Test
{
public : Test()
{
}
};
int main()
{
Test object;
return(0);
}
If my understanding is correct, the constructor isn't responsible for
the allocation of the memory for the object, so how does the actual
memory allocation occur?
What exactly happens beneath the following statement,
Test object;
Thanks in advance!!!
'object' is said to have automatic storage duration.
int main ()
{
auto Test object;
}
A C++ compiler would generate the necessary processor instructions to
allocate storage from a pre-allocated memory area called the stack; a
memory area typically directly accessible to the processor. A C++
compiler would also generate the necessary processor instructions to
free the memory.
int main ()
// Instructions to allocate memory for 'object'.
{
Test object;
}
// Instructions to free memory for 'object'.
int main ()
// Instructions to allocate memory for 'object'.
{
Test object;
// Instructions to allocate memory for 'object2'.
{
Test object2;
}
// Instructions to free memory for 'object2'.
}
// Instructions to free memory for 'object'.
int main ()
// Instructions to allocate memory for 'object'.
{
Test object;
// Instructions to allocate memory for 'object2'.
{
Test object2;
// Instructions to allocate memory for 'object3'.
{
Test object3;
}
// Instructions to free memory for 'object3'.
}
// Instructions to free memory for 'object2'.
}
// Instructions to free memory for 'object'.
The constructor is then called to initialize the memory to a known
internal state/assumption.
Some people want to hide all the underlying pieces, but this is how it
most often will be implemented, to this date, I'm 99% sure. Although,
all you need to know at one level is that C++ deals with allocating and
freeing the memory (thus the name automatic storage duration). So once
object2 and object3 are out of scope, they no longer exist.
What's really going to bake your noodle later on is--what happens if
there's not enough memory? Some systems crash. Others deal with it in
one way or another. It's beyond the scope of C++. If you really care
about control, you would research this on each target system. Though,
statistically, how often would this occur?
.
|
|
|
|
| User: "Rolf Magnus" |
|
| Title: Re: creation of an object |
03 Feb 2008 07:36:02 AM |
|
|
Rahul wrote:
Hi Everyone,
I have the following code,
class Test
{
public : Test()
{
}
};
int main()
{
Test object;
return(0);
}
If my understanding is correct, the constructor isn't responsible for
the allocation of the memory for the object, so how does the actual
memory allocation occur?
What exactly happens beneath the following statement,
Test object;
The C++ standard doesn't say much about what happens. First, memory is
reserved for the object in some implementation-specific way, then that raw
memory is turned into a real object by the constructor.
.
|
|
|
|

|
Related Articles |
|
|