Grabbing the return value before it's erased.

This is a discussion on Grabbing the return value before it's erased. within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by vart I suppose you are allocating memory in your push_xxx functions For such classes you have to ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    Quote Originally Posted by vart
    I suppose you are allocating memory in your push_xxx functions

    For such classes you have to provide copy constructors

    If you don't - the compiler creates one for you - just using member by member assignments...

    In this case all pointers point to the same memory as the original class instance,
    And after the original class is destroyed - your copy has dangling pointer here and there...

    With your function you invoke copy-constructor in the return statement from the the local variable to the temp-variable being returned...

    After that the operator = is invoked to copy values from the temp variable to the newly created var in the calling function. It means this operator also should be provided
    I think that's the problem. So I'll have to find another way around it.

  2. #17
    Registered User
    Join Date
    Dec 2006
    Posts
    17
    Quote Originally Posted by vart
    I suppose you are allocating memory in your push_xxx functions

    For such classes you have to provide copy constructors

    If you don't - the compiler creates one for you - just using member by member assignments...

    In this case all pointers point to the same memory as the original class instance,
    And after the original class is destroyed - your copy has dangling pointer here and there...
    Spot on, I was just trying to put that into words myself

    You can't declare something on the stack then expect the code to just know that you've extended it onto the heap when you go and pass it by value :P

    Personally I'd declare the list on the heap and pass a pointer. You're just creating overhead by copying everything constantly anyway... I suppose that depends on the purpose behind your program to some extent though.

  3. #18
    Registered User
    Join Date
    Dec 2006
    Posts
    17
    Quote Originally Posted by hardi
    I think that's the problem. So I'll have to find another way around it.
    Create a copy constructor that handles the heap-based allocations you made with new() or keep it all on the heap and pass a pointer around.

  4. #19
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,458
    Quote Originally Posted by hardi
    I think that's the problem. So I'll have to find another way around it.
    Note that for classes using dynamic memory allocation directly you should always provide copyConstructor and operator =
    If you think you don't need them and don't want to implement - a good practice is to create empty functions for them and make them private.

    In this case if you still use one of the above by mistake - compiler will note you
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  3. sort linked list using BST
    By Micko in forum C Programming
    Replies: 8
    Last Post: 10-04-2004, 02:04 PM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 09:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21