Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a reference

Tags:

c++

Is this valid? An acceptable practice?

typedef vector<int> intArray;

intArray& createArray()
{
    intArray *arr = new intArray(10000, 0);

    return(*arr);
}


int main(int argc, char *argv[])
{

    intArray& array = createArray();

    //..........

    delete &array;

    return 0;
}
like image 827
Newton Falls Avatar asked Jul 13 '10 03:07

Newton Falls


People also ask

How do you remove references from a document?

To delete the reference in the document, select the citation from the document and click on Delete Once you have deleted all the citations from the document click on References< Manage Sources< highlight the citation from the current list and click Delete.

How do I remove a reference from endnotes?

Click and drag references across to Trash in the left pane. Highlight the references, holding down the Ctrl key to select more than one, go to the References menu and select Move references to Trash. Highlight the references, holding down the Ctrl key to select more than one, and press the Delete key.

Can you edit references?

Edit citations or references in your Word document To do this, make sure your cursor is in the citation that needs changing, then go to the EndNote toolbar. Click on the "Edit Citation(s)" button. A pop-up box will open. The bottom field in the box is called Pages.


4 Answers

The behavior of the code will be your intended behavior. Now, the problem is that while you might consider that programming is about writing something for the compiler to process, it is just as much about writing something that other programmers (or you in the future) will understand and be able to maintain. The code you provided in many cases will be equivalent to using pointers for the compiler, but for other programmers, it will just be a potential source of errors.

References are meant to be aliases to objects that are managed somewhere else, somehow else. In general people will be surprised when they encounter delete &ref, and in most cases programmers won't expect having to perform a delete on the address of a reference, so chances are that in the future someone is going to call the function an forget about deleting and you will have a memory leak.

In most cases, memory can be better managed by the use of smart pointers (if you cannot use other high level constructs like std::vectors). By hiding the pointer away behind the reference you are making it harder to use smart pointers on the returned reference, and thus you are not helping but making it harder for users to work with your interface.

Finally, the good thing about references is that when you read them in code, you know that the lifetime of the object is managed somewhere else and you need not to worry about it. By using a reference instead of a pointer you are basically going back to the single solution (previously in C only pointers) and suddenly extra care must be taken with all references to figure out whether memory must be managed there or not. That means more effort, more time to think about memory management, and less time to worry about the actual problem being solved -- with the extra strain of unusual code, people grow used to look for memory leaks with pointers and expect none out of references.

In a few words: having memory held by reference hides from the user the requirement to handle the memory and makes it harder to do so correctly.

like image 169
David Rodríguez - dribeas Avatar answered Oct 16 '22 10:10

David Rodríguez - dribeas


Yes, I think it will work. But if I saw something like this in any code I worked on, I would rip it out and refactor right away.

If you intend to return an allocated object, use a pointer. Please!

like image 32
Mark Ransom Avatar answered Oct 16 '22 11:10

Mark Ransom


It's valid... but I don't see why you'd ever want to do it. It's not exception safe, and std::vector is going to manage the memory for you anyway. Why new it?

EDIT: If you are returning new'd memory from a function, you should return the pointer, lest users of your function's heads explode.

like image 38
Billy ONeal Avatar answered Oct 16 '22 09:10

Billy ONeal


Is this valid?

Yes.

An acceptable practice?

No.

This code has several problems:

  • The guideline of designing for least surprising behavior is broken: you return something that "looks like" an object but must be deleted by the client code (that should mean a pointer - a reference should be something that always points to a valid object).

  • your allocation can fail. Even if you check the result in the allocating function, what will you return? An invalid reference? Do you rely on the allocation throwing an exception for such a case?

As a design principle, consider either creating a RAII object that is responsible for managing the lifetime of your object (in this case a smart pointer) or deleting the pointer at the same abstraction level that you created it:

typedef vector<int> intArray;

intArray& createArray()
{
    intArray *arr = new intArray(10000, 0);

    return(*arr);
}

void deleteArray(intArray& object)
{
    delete &object;
}

int main(int argc, char *argv[])
{
    intArray& array = createArray();
    //..........
    deleteArray(array);
    return 0;
}

This design improves coding style consistency (allocation and deallocation are hidden and implemented at the same abstraction level) but it would still make more sense to work through a pointer than a reference (unless the fact that your object is dynamically allocated must remain an implementation detail for some design reason).

like image 20
utnapistim Avatar answered Oct 16 '22 10:10

utnapistim