By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,437 Members | 1,337 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,437 IT Pros & Developers. It's quick & easy.

unable to understand constructor behavior

P: n/a
neo
I made a console based application in vs2k5. I made a class with the
name "A" which has one function with the name of "function" and
has one member integer variable initialized with 99; its code is
following.

class A
{
private:
int x;
public:
A( )
{
x = 99;
cout<<"const of class A"<<endl;
}
~A( )
{
cout<<"dest of class A"<<endl;
}
void function( )
{
cout<<"value of x : "<<x<<endl;
}
};

I wrote few lines of code in main function, these are following

A * ptr = &(A());
ptr->function( );

I have no idea about A(), but what I right now understand about it is,
that it's a object created to execute its constructor and immediately
gets destroyed when its out of its constructor and that it is allocated
on stack. Now even if it is destroyed, I still have a reference (as a
pointer) to it in form of a pointer. And now if I use that pointer to
call its methods I can easily do so and it behaves exactly in the way
as if it were never destroyed from the memory. Please explain this
behavior.

Note: In writing this code I also observed that when a stack based
object is destroyed, its destruction behaves a little bit different
than the destruction of a heap base objects (its just an observation
and I maybe wrong about it). For example, if the above object was
created on a heap (by using *new*), its constructor would have set its
internal x variable to 99. I could have got a pointer to that variable
and use it to display its value after having called a delete on A's
pointer, but I would not get the value of x as it was when the object
was alive. That's apparently not the case with stack based allocation
because even after A's destruction from the stack, I continue to get
the actually value of x as it was when the object was alive.

Regards,

-aims

Dec 29 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
neo wrote:
I made a console based application in vs2k5. I made a class with the
name "A" which has one function with the name of "function" and
has one member integer variable initialized with 99;
Just for the record: You didn't initialize with 99, you assigned 99 to it.
Non-static member variables can only be initialized in the initializer
list.
its code is following.

class A
{
private:
int x;
public:
A( )
{
x = 99;
cout<<"const of class A"<<endl;
}
~A( )
{
cout<<"dest of class A"<<endl;
}
void function( )
{
cout<<"value of x : "<<x<<endl;
}
};

I wrote few lines of code in main function, these are following

A * ptr = &(A());
ptr->function( );

I have no idea about A(), but what I right now understand about it is,
that it's a object created to execute its constructor and immediately
gets destroyed when its out of its constructor and that it is allocated
on stack.
Yes, except that it's called "automatic storage" in C++ instead of "stack".
Now even if it is destroyed, I still have a reference (as a
pointer) to it in form of a pointer. And now if I use that pointer to
call its methods I can easily do so and it behaves exactly in the way
as if it were never destroyed from the memory. Please explain this
behavior.
The behavior is formally undefined. That means that anything can happen, and
that includes the behavior your observed.
Note: In writing this code I also observed that when a stack based
object is destroyed, its destruction behaves a little bit different
than the destruction of a heap base objects (its just an observation
and I maybe wrong about it). For example, if the above object was
created on a heap (by using *new*), its constructor would have set its
internal x variable to 99. I could have got a pointer to that variable
and use it to display its value after having called a delete on A's
pointer, but I would not get the value of x as it was when the object
was alive. That's apparently not the case with stack based allocation
because even after A's destruction from the stack, I continue to get
the actually value of x as it was when the object was alive.
Again, "undefined behavior" means anything can happen. It's not really
useful to try to understand why exactly the behavor is like it is.

Dec 29 '06 #2

P: n/a
Hello!

neo wrote:
[...]
>
I have no idea about A(), but what I right now understand about it is,
that it's a object created to execute its constructor and immediately
gets destroyed when its out of its constructor and that it is allocated
on stack. Now even if it is destroyed, I still have a reference (as a
pointer) to it in form of a pointer. And now if I use that pointer to
call its methods I can easily do so and it behaves exactly in the way
as if it were never destroyed from the memory. Please explain this
behavior.
I do believe the behaviour you're asking about is, as they say,
undefined. You're just lucky that it just happens to be turning out the
same as if the object hadn't been destroyed.
Note: In writing this code I also observed that when a stack based
object is destroyed, its destruction behaves a little bit different
than the destruction of a heap base objects (its just an observation
and I maybe wrong about it). For example, if the above object was
created on a heap (by using *new*), its constructor would have set its
internal x variable to 99. I could have got a pointer to that variable
and use it to display its value after having called a delete on A's
pointer, but I would not get the value of x as it was when the object
was alive. That's apparently not the case with stack based allocation
because even after A's destruction from the stack, I continue to get
the actually value of x as it was when the object was alive.
That's entirely consistent with the behaviour being undefined. Being
undefined means, amongst other things, that they don't have to behave
the same way.

Simon

--
What happens if I mention Leader Kibo in my .signature?
Dec 29 '06 #3

P: n/a

neo wrote:
I made a console based application in vs2k5. I made a class with the
name "A" which has one function with the name of "function" and
has one member integer variable initialized with 99; its code is
following.
#include <iostream>
#include <ostream>
>
class A
{
private:
int x;
public:
A( )
{
x = 99;
cout<<"const of class A"<<endl;
}
A() : x(99) { std::cout << "A()\n"; }
~A( )
{
cout<<"dest of class A"<<endl;
}
void function( )
{
cout<<"value of x : "<<x<<endl;
}
};

I wrote few lines of code in main function, these are following

A * ptr = &(A());
ptr->function( );

I have no idea about A(), but what I right now understand about it is,
that it's a object created to execute its constructor and immediately
gets destroyed when its out of its constructor and that it is allocated
on stack. Now even if it is destroyed, I still have a reference (as a
pointer) to it in form of a pointer. And now if I use that pointer to
call its methods I can easily do so and it behaves exactly in the way
as if it were never destroyed from the memory. Please explain this
behavior.
The above is undefined behaviour. What the result might be is
irrelevant. Nobody cares.
>
Note: In writing this code I also observed that when a stack based
object is destroyed, its destruction behaves a little bit different
than the destruction of a heap base objects (its just an observation
and I maybe wrong about it). For example, if the above object was
created on a heap (by using *new*), its constructor would have set its
internal x variable to 99. I could have got a pointer to that variable
and use it to display its value after having called a delete on A's
pointer, but I would not get the value of x as it was when the object
was alive. That's apparently not the case with stack based allocation
because even after A's destruction from the stack, I continue to get
the actually value of x as it was when the object was alive.
That doesn't matter, its still UB. Nobody cares what one compiler or
platform does with either the stack or heap allocation after
destruction has been invoked. Your client or employer might care: but
thats another issue (debugging UB costs money).
>
Regards,

-aims
Dec 29 '06 #4

P: n/a

neo wrote in message
<11**********************@i12g2000cwa.googlegroups .com>...
>I made a console based application in vs2k5. I made a class with the
name "A" which has one function with the name of "function" and
has one member integer variable initialized with 99; its code is
following.

class A{
int x;
public:
A( ){
x = 99;
cout<<"const of class A"<<endl;
}
~A( ){
cout<<"dest of class A"<<endl;
}
void function( ){
cout<<"value of x : "<<x<<endl;
}
};

I wrote few lines of code in main function, these are following

A * ptr = &(A());
ptr->function( );
int main(){
A *ptr = &( A() );

int arry[1000] = {0};
char arryc[] = "klsdiuiovisdoioisdoiirj";

ptr->function();

} // main()

Still work?

--
Bob R
POVrookie
Dec 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.