How to use new

This is a discussion on How to use new within the C++ Programming forums, part of the General Programming Boards category; The situation is a lot more complicated than you think. In your first example, the constructor you defined is first ...

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    The situation is a lot more complicated than you think.

    In your first example, the constructor you defined is first called on the line where you create the base object. It allocates an integer (e.g. at 0x00402222) and sets its value.

    Then you call fun() and pass the object. Since it's a value parameter, a copy of the object is created, and the copy copy constructor is called for the new object. Since you don't have one defined, this means the default one is used, which just copies the class bit by bit. The new object now contains a pointer which points to the same integer (at 0x00402222) as the object in main().

    When fun() returns, the object you passed is destroyed. Its destructor is called and it deletes the integer at 0x00402222. That memory is no longer valid.

    Finally, main() returns as well, and the object there is destroyed as well. Its destructor is called. And since its pointer also points to 0x00402222, it attempts to delete memory that has already been deleted, probably leading to a crash in debug mode.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #17
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    when will the desructor function base::~base will be called for b1 ?
    Is it at end the function fun(base b1) ?
    Or the end of main() ?
    b1 is a parameter of a function. Function parameter names are local variables of the function, and they are initialized with the values you send to the function. That is why the types of the values you send to a function must match the types of the parameter variables. When the function ends, all the local variables are destroyed. So, the destructor for b1 is called when the function ends.

    CornedBee described the hazards of passing objects with dynamically allocated memory to functions "by-value": the memory gets deleted when the function ends.
    Last edited by 7stud; 11-25-2005 at 03:23 AM.

  3. #18
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    Thanks

    Thanks CornedBee and 7stud,

    I now understand your post CornedBee. Thank you, I havent think about that.

    I figured that the desractour when you pass by value will be called when the function end.
    But my book , consisted that it will happen only at end of the program.
    I needed this confiramtion.
    Thank you.
    Last edited by dude543; 11-26-2005 at 06:50 PM. Reason: more

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

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