unable to understand constructor behavior

N

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
 
R

Rolf Magnus

neo said:
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.
 
S

Simon G Best

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
 
S

Salt_Peter

neo said:
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 said:
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).
 
B

BobR

neo wrote in message
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?
 

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
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top