unable to understand constructor behavior

Discussion in 'C++' started by neo, Dec 29, 2006.

  1. neo

    neo Guest

    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
     
    neo, Dec 29, 2006
    #1
    1. Advertising

  2. neo

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Dec 29, 2006
    #2
    1. Advertising

  3. neo

    Simon G Best Guest

    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?
     
    Simon G Best, Dec 29, 2006
    #3
  4. neo

    Salt_Peter Guest

    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
     
    Salt_Peter, Dec 29, 2006
    #4
  5. neo

    BobR Guest

    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?

    --
    Bob R
    POVrookie
     
    BobR, Dec 30, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nitin Goyal
    Replies:
    3
    Views:
    721
  2. Replies:
    3
    Views:
    267
  3. Generic Usenet Account
    Replies:
    10
    Views:
    2,282
  4. Sreenivas

    dont understand constructor...

    Sreenivas, Dec 20, 2007, in forum: Java
    Replies:
    7
    Views:
    322
  5. Sanchit

    unable to understand this typedef

    Sanchit, Mar 20, 2008, in forum: C Programming
    Replies:
    1
    Views:
    268
    Eric Sosman
    Mar 20, 2008
Loading...

Share This Page