Thread: CConstructor Infinite Recursion!

  1. #61
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The containers stores copies of what you feed them, so obviously it calls the copy constructor to make copies of what you put in.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #62
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Elysia View Post
    The containers stores copies of what you feed them, so obviously it calls the copy constructor to make copies of what you put in.
    Yeah i know, except the declaration is:

    push_back(const T&)

    it only copies the reference of the object. Since it isn't copying the object itself
    there is no reason for the invocation copy constructor.
    You ended that sentence with a preposition...Bastard!

  3. #63
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The actual copying happens later. Typically the code creates a new T, which is given its value from what you put in. You need to do some more digging.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #64
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Elysia View Post
    The actual copying happens later. Typically the code creates a new T, which is given its value from what you put in. You need to do some more digging.
    so a new object is created, the copy constructor copies the values to the new object. The reference of the new T is passed to push_back.... ?

    that sounds like a waste...

    i tried going deeper. but i couldn't read the _Insert function definition.
    You ended that sentence with a preposition...Bastard!

  5. #65
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    It is something like that:

    Your program:
    1. Creates your A object, initializes it, etc
    2. Pushes reference to A (its address)
    3. Calls push_back

    push_back:
    1. Pops the reference passed
    2. Creates new node for the list
    3. It cannot store reference in the list, because the object, created by your program, may be destroyed after returning from push_back, thus it needs to place a copy of A in the node

    Your program:
    4. When it gets back from the push_back, the reference passed is destroyed, the copy of A in the node remains, your program may destroy the original object

    What cannot you understand else?

  6. #66
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by kmdv View Post
    It is something like that:

    Your program:
    1. Creates your A object, initializes it, etc
    2. Pushes reference to A (its address)
    3. Calls push_back

    push_back:
    1. Pops the reference passed
    2. Creates new node for the list
    3. It cannot store reference in the list, because the object, created by your program, may be destroyed after returning from push_back, thus it needs to place a copy of A in the node

    Your program:
    4. When it gets back from the push_back, the reference passed is destroyed, the copy of A in the node remains, your program may destroy the original object

    What cannot you understand else?
    That was pretty clear thanks.
    so at push_back stage 3, the CCtor is invoked. Fair enough, just thought it was a bit odd.
    Thanks.
    You ended that sentence with a preposition...Bastard!

  7. #67
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Again, a reference its not an address! You can't push back the address of a reference! Nor can you pop it!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #68
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Elysia View Post
    Again, a reference its not an address! You can't push back the address of a reference! Nor can you pop it!
    11: References & the Copy-Constructor

    "A reference is a fancy pointer..."

    an address of the object is passed to the variable, except the compiler does the job of dereferencing it for us.
    You ended that sentence with a preposition...Bastard!

  9. #69
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is a random site you found on internet a trust worthy source? Please cite the standard instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #70
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Elysia View Post
    Is a random site you found on internet a trust worthy source? Please cite the standard instead.
    Ok i can't find the website for a C++ standard. But I am sure Microsoft no matter how cr*p their software is, use it.

    Reference Operator: & (C++)

    main point:
    "A reference holds the address of an object but behaves syntactically like an object"
    Last edited by Eman; 12-22-2010 at 11:19 AM.
    You ended that sentence with a preposition...Bastard!

  11. #71
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Again, how references are implemented is implementation defined. Microsoft has chosen the way you described. That doesn't mean all other compiler vendors will.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #72
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Elysia View Post
    Again, how references are implemented is implementation defined. Microsoft has chosen the way you described. That doesn't mean all other compiler vendors will.
    yeah I am trying to unlearn that .

    By the way, maybe what kmdv meant by
    "pops the reference"

    was actually
    "makes a reference?"
    Last edited by Eman; 12-22-2010 at 11:31 AM.
    You ended that sentence with a preposition...Bastard!

  13. #73
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Eman View Post
    What do you mean, what is useless?
    Because it's like the hotel california...

    You can put a string in it, but can never get it out again.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #74
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    I was thinking about pushing / poping parameters from the stack. I should have mentioned the stack. Parameters can also be passed via registry or be eliminated. The point was to explain how it works and give a brief overview. I prefer explaining things giving a concrete example (here a specific platform) as I said it is 'something' like that. Standard can be very confusing.
    Last edited by kmdv; 12-22-2010 at 12:43 PM.

  15. #75
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Em. Does anyone know why a pushing an object into a list invokes the copy constructor?
    I just had a glance at the api and noticed it takes a reference to an object. So it shouldn't invoke it, but it does?
    Now, remember, the STL requires copyable objects. So what does this mean? Well, you were already told that this:
    Code:
    void push_back ( const T &x )
    {
        T newNode( x );
    }
    would be virtually the same as copying it here:
    Code:
    void push_back ( T x )
    Hopefully you understand why it's important that the STL is in charge of deleting the stuff they contain. That's why you bother with copies at all. Additionally, I think the STL only uses the first version because push_back can accept the return value of another function, which is nice, and the references avoid unnecessary copying. There is a name for what exactly happens in a situation like that which I don't care to remember, but I think that is the design rationale at work here.

    Optimizing compilers may fail to make a real difference between either version but remember vendors have to support a standard interface. So in summary, if you want to use the STL containers, have them contain copyable and assignable objects. Now just keep in mind what you've learned about the copy constructor and assignment operator (specifically, when you have to write your own) and you should be OK.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. convert Recursion to linear can it be done
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2008, 02:58 AM
  3. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  4. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  5. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM