August 10th, 2003, 05:09 AM
deleting references from the free store.
ok, this book i am reading says the references cannot be null, but leaving them can create memory leakage. so how exactly do i delete them and prevent memory leakage? heres what the book says but i do not understand it:
The third workable solution, and the right one, is to declare the object in the calling function and then to pass it to TheFunction() by reference.
The best I can interprete it is to create the object within the function, and then return the value of that object with a reference to the object. how does this delete the reference? is it that, since it points to a local variable, it gets deleted when the function ends? a lot of that i just thought up of now :D So did I get that right? I dont want to guess and get it wrong because the book scares me in the ways it talks about invalid programs :eek:
August 10th, 2003, 05:44 AM
Can you show us the context in which it says that leaving references can create memory leakage?
August 10th, 2003, 09:48 AM
>> To prove that the reference declared in main() is referring to the object put on the free store in TheFunction(), the address of operator is applied to rCat(the reference object). Sure enough, it displays the address of the object it refers to and this matches the address of the object on the free store.
So far, so good. But how will that memory be freed? You can't call delete on the reference.
So I figure you have to do something that will have the same effect as delete, which is freeing up memory on the free store, correct?
It relates to an example but I hope you can figure out what it means. It just calls the address operator on an object and a reference, and it displays the same address, meaning that the reference at least refers to something on the free store.