views:

264

answers:

3

is the following function OK:

void DoSomething(auto_ptr< … >& a)....
+1  A: 

Yes you can. But this isn't a very good question. Are you having problems? What are you doing with the the pointer in the function? All your questions here tend to show a similar reluctance to provide sufficient information for us to provide good answers.

And whether you should be using auto_ptrs in the first place is another issue.

anon
I want to use auto_ptr to prevent memory leaks. Speed issues are less important here. From what i understand, i should use smart pointers whenever possible (unless speed is to important) - correct me if i am wrong.You told me already that my questions lack information and i am aware of this. In this case, it is simply a yes/no question (i did not try to do this yet)
amitlicht
I agree with Neil Butterworth; it is not a very well thought out question. Syntactically, the code snippet is OK (if we assume that the appropriate header is included) and that is all that one can answer without asking you about other possible contexts and issues that may be in your mind. It's like asking if this program is OK: int main(){ return 0; }. Yes if asking about syntax; no if the purpose (as described by a spec) of the program is to calculate the sum of two unsigned integers input at the command line by a user.
Sam
+1  A: 

You can do it but I'm not sure why you would want to do it.

If you're using the auto_ptr to indicate ownership of the ptr (as people generally do), then you only need to pass the auto_ptr to a function if you want to transfer ownership of the ptr to the function, in which case you would pass the auto_ptr by value:

void DoSomething(auto_ptr<int> a)

So any code calling DoSomething relinquishes ownership of the ptr:

auto_ptr<int> p (new int (7));
DoSomething (p);
// p is now empty.

Otherwise just pass the ptr by value:

void DoSomething(int* a)
{...}

...

auto_ptr<int> p (new int (7));
DoSomething (p.get ());
// p still holds the ptr.

or pass a ref to the pointed to object:

void DoSomething(int& a)
{...}

...

auto_ptr<int> p (new int (7));
DoSomething (*p);
// p still holds the ptr.

The second is usually preferable as it makes it more explicit that DoSomething is unlikely to attempt to delete the object.

jon hanson
passing the pointer by value is not safe - memory leaks could occur.creating the object without 'new' means that the object will be on the stack - not sure it is the best approach either.
amitlicht
@eriks: Memory leaks *can* occur in many situations; even if you pass an `auto_ptr` by reference, someone could call `delete ptr.get()` which would be worse. Passing pointers by value passes no information about transfer of ownership or the correct form of deletion so - in the absence of documentation to the contrary - I wouldn't expect the callee to delete the object pointed to.
Charles Bailey
@eriks: You won't get memory leak, you'll just get dangling pointers.
KennyTM
@eriks: I've expanded my examples to hopefully clarify.
jon hanson
A: 

Why don't you just try and see what happens???

gotch4
Reference counting bugs don't always appear right away.
Potatoswatter
@Potatoswatter: auto_ptr is not reference counted.
Nemanja Trifunovic
@Nemanja: d'oh, not smart pointer. Well, this whole question is somewhat obscure as auto_ptrs are not typically passed around like that.
Potatoswatter