Is it possible to use auto pointer to point to an existing variable?
I tried the following an get segfault:
I know that I can use new to allocate memory for "a" then assign the value of "c" to a, but that doesn't mean that 'a' point to c:
int c = 0;
std::auto_ptr<int> a (&c);
std::auto_ptr<int> a ( new int(c));
Of course you'll get a segfault. It would be the same as writing:which is of course ridiculous. You can't delete something that was not allocated dynamically.
The purpose of an auto_ptr is to delete something automatically without having to remember to do it yourself.
thanks iMalc for the answer.