Is it okay( and legal) to delete a pointer that has been passed as a function argument such as this:
#include<iostream>
class test_class{
public:
test_class():h(9){}
int h;
~test_class(){std::cout<<"deleted";}
};
void delete_test(test_class* pointer_2){
delete pointer_2;
}
int main(){
test_class* pointer_1;
while(true){
pointer_1 = new test_class;
//std::cout<<pointer_1->h;
delete_test(pointer_1);
}
}
This compiles fine now, but I just want to make sure it'll alwa开发者_运维技巧ys be that way.
It will always compile without error.
Whether it's a good thing to pass a pointer into a function and delete it in that function is potentially another story, depending on the specifics of your program.
The main idea you need to consider is that of "ownership" of the pointed-to data. When you pass that pointer, does the calling function have ownership of the data being passed in? i.e. is it in the only place that this data can be referenced from? Are you giving up ownership of the pointed-to-data, with no chance that the calling function is ever going to reference the data again? If so, then you must delete it.
If the calling function might reference the data again, then you must not delete it.
If there are other references to the data through various data structures, then it's not safe to delete this data unless you have some discipline in place in your code to ensure that you will never reference the data again from those places. This is hard to do, and is the source of many programming bugs.
C++ tr1's shared_ptr<> is a smart pointer that helps in these kinds of situations - it manages this ownership concept by keeping a reference count that tracks the number of references to the data. If the reference count is 1, then there is 1 clear owner. If the reference count is larger than 1, then ownership is shared. If the reference count is 0, then there are no more references to the data, and shared_ptr<> will delete it when the shared_ptr<> destructor is called.
Yes, this is valid.
This is commonly done in C (with malloc and free instead of new and delete, obviously). In C++, it is generally preferable to use other memory management idioms like RAII, if you can.
Yes it is legal in C++, but doing this is not generally considered as a good practice. It is always better for the class which performed new
to delete
the same.
Yes, this is perfectly legal. You can delete
a pointer from wherever, as long as it points to some object allocated on the heap (or is equal to 0
).
Whether the caller expects their object to be deleted by the function, that's another question.
That's completely legal, though in such a case it's probably better to manage memory ownership with something like boost::shared_ptr.
It is perfectly legal to do that. You must make sure that the pointer isn't used in the caller after that point. In general, the name of the function doing the delete should indicate that is what is happening (e.g., contain delete, release, free, etc). Another potential problem is making sure that the data pointed to was allocated with new and not new[].
It is valid and can be very useful when writing a cleanup method for the object, although 9/10 times you'd want to put the cleanup logic into the destructor.
One good reason though to write a separate cleanup would be if you want to keep the object "alive" but not used for awhile, maybe in an array or pool of objects that you pull from occasionally when you need a new one without wanting the constructor overhead.
If you are going to have pointers passed in you should check to make sure they aren't null to avoid any undefined behaviour.
Probably a bit late to the discussion, but I wanted to add a clarification: the function only takes a copy of the pointer ( fnc(int* a) is a pass by value) thus you're deleting the local copy of the pointer you've passed and the original pointer will remain available.
That code probably was optimized by the compiler to avoid unnecessary allocation and deletion which allowed it to work.
精彩评论