freeing memory when passing objects

Hello, I am currently learning about passing objects to functions from my book, and I am wondering a few things about what it has said. I am hoping someone could help me out with a few questions.

The book says when an object is passed by reference, "because a copy is made, if the original object allocates some system resource (such as memory) when it is created and frees that resource when it is destroyed, then its local copy inside the function, will free that same resource when its destructor is called. This is a problem because the original object is still using this resource "

Just to note, it does go on to talk about passing by reference and copy constructors, this is just explaining why these two are needed.

So I understand the basic concept of this, but I am wondering about the allocating of the memory part. If I am allocating memory in the new copy object wouldnt it be at a new memory location. Or is it at the same memory location because it is a bitwise copy of the original?

And just while on the subject, when it talks about allocating in these terms, does that refer to the 'new' keyword. Or can that mean something like declaring an int? I have not covered the new keyword yet, but have seen it used to create memory areas (sorry not really sure of the correct terminology yet)
it is saying that if you are passing a very poorly coded 'dumb' object around, it will screw up.
this will NOT happen if you pass in, for example, a <vector> or <string>: these objects were coded not to be idiotic and will NOT do what the book said.

but say you did this:
1
2
3
4
5
6
struct derp
{
    int * ip;
    derp() {ip = new int[100];}
    ~derp(){delete[] ip;}
}


you did not manually add assignment operators, so when it does a copy, which uses assignment, it will use the default one. The default one will just copy the pointer, not make a new one. But when it is destroyed, it will deallocate it, and that pointer was being used by both the copy and the original. Its a bug / badly coded object across the board: it is also true that if you had a copy and modified the 'array' under the copy, you modified the data in the original as well, its the SAME data. This kind of class can introduce all kinds of 'fun' bugs due to this problem.

The fix for it is to do a rule of 3 or rule of 5 for the class if you need to make copies of it. (you may also be able to do some sort of static class member variable voodoo that can 'know' if the current object called the constructor and if it did not, do not call the destructor, but its a lot of smoke and mirrors to solve a part of a bigger problem).

you should be seeing these rules soon in your studies. And the real fix for it is to not make classes that manhandle memory if you can avoid it, eg like using a vector instead of a pointer above would solve the problem without any extra nonsense. If doing memory yourself is absolutely a must, then you need to be very, very careful with it.

the reason it does not allocate new memory is the copying does not call the normal constructor, it calls the copy constructor, which does not exist (was not programmer written) so it generates one and the generated one is not correct for this kind of object.
Or is it at the same memory location because it is a bitwise copy of the original?
almost this. As you are thinking, yes, this is why, but I am not 100% sure that all possible automatically generated functions would be purely bitwise copies ... I need to think about that one.

if you want to see this in action, use my struct, add a cout statement to the destructor, and play with it. if you call it in a function that does a non-reference parameter of the struct, it will destroy the object twice (you can see in the print statements).
Last edited on
Just wanted to thank you for the reply, I am still trying to fully understand it all. I am going to run your code and do as you suggest, I think that will def help.
The book says when an object is passed by reference, "because a copy is made, if the original object allocates some system resource...

That doesn't make sense.
One purpose of passing by reference is to avoid making copies.
What book are you using? Passing an object by value will cause a copy to be made (deep copy if the object uses dynamic memory and the copy is implemented properly, shallow copy otherwise by default). Passing by reference does not cause a copy. If you pass say an int, it doesn't matter if you copy by ref or value, but if the size of the passed data is greater than the size of a pointer (ref is implemented behind the scene by passing a pointer), then it is quicker to pass by reference rather than by value.

Sometimes if are going to create a temporary copy of the passed variable so that eg it can be changed without effecting the calling value, then you do pass by value to get the copy.

If I am allocating memory in the new copy object wouldnt it be at a new memory location. Or is it at the same memory location because it is a bitwise copy of the original?


Is this for a copy constructor or for a copy assignment? If for a copy constructor, you don't already have a memory location as you are creating a new object. If for a copy assignment, then yes the new memory will be at a new memory location. The copy assignment code should delete the old memory. If the class uses dynamic memory then you most definitely don't do a bitwise copy (shallow copy), but a deep copy.

How is your book detailing to do copy constructor and copy assignment - there are different ways.
Last edited on
Topic archived. No new replies allowed.