For Programmers: Free Programming Magazines  


Home > Archive > VC STL > February 2006 > possiblity to assign any arbitrary pointer to a specialized auto_ptr









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author possiblity to assign any arbitrary pointer to a specialized auto_ptr
ms@nogga.de

2006-02-10, 7:03 pm

Hello,

is it only me, or is it really possible to assign any arbitrary pointer
to a specialized auto_ptr? E.g. the following code will compile in
vs2005 with any problems:

// assigning a pointer in the constructor
std::auto_ptr<CDerived> test1 (new int);
std::auto_ptr<CDerived> test2 (new double);

// assigning a pointer in the copy constructor
std::auto_ptr<CDerived> test3 = new int;
std::auto_ptr<CDerived> test4 = new double;


The reason for this behavior is the implicit conversion via the
auto_ptr_ref class, which casts away the type information into a void *
and from there back into the concrete type in the auto_ptr classes again.

template<class _Ty>
struct auto_ptr_ref
{ // proxy reference for auto_ptr copying
auto_ptr_ref(void *_Right)
: _Ref(_Right)
{ // construct from generic pointer to auto_ptr ptr
}

void *_Ref; // generic pointer to auto_ptr ptr
};


With this definition it is e.g possible to write:

auto_ptr_ref <double> d1 = new int;
auto_ptr_ref <double> d2 = new char;
d1 = d2;

while it is not legal to write

auto_ptr_ref <int> i1 = new int;
d1 = i1;


This conversion via the void pointer allows that you can stuff any
pointer into a concrete auto_ptr class.

I came across this problem in the following code:

auto_ptr <CBase> MakeClass ()
{
return new CDerived ();
}

void main ()
{
auto_ptr <CBase> test (MakeClass ());
}


This was legal code in VC6 and will result in a compile error in VC7.1.
In VS2005 this code will compile again, but will result in a runtime
error, since the implicit conversion via the auto_ptr_ref will result in
a double dereferencing in the constructor of auto_ptr again.

auto_ptr(auto_ptr_ref<_Ty> _Right) _THROW0()
{ // construct by assuming pointer from _Right auto_ptr_ref
_Ty **_Pptr = (_Ty **)_Right._Ref;
_Ty *_Ptr = *_Pptr;
*_Pptr = 0; // release old
_Myptr = _Ptr; // reset this
}

I know, the function should have been written to return a
auto_ptr<CBase> in the first place. But I expect this is a common
programming error, even if the compiler will not through any warning or
error about this.


Do I get something completely wrong here, or is this a bug in vs2005.

Best regards
Dirk
ms@nogga.de

2006-02-13, 7:05 pm

Ulrich Eckhardt schrieb:
> ms@nogga.de wrote:
>
> Just for your info, both of the above should not compile, the first couple
> because of a type mismatch the second one also because auto_ptr has an
> explicit ctor.


That is exactly my point. It does compile on VS2005.

>
> This is ill-formed, too, because auto_ptr's ctor is explicit (it should
> be...) so you have to explicitly create the auto_ptr.


Yes, that is true. But I would say it is common programming mistake and
exactly the reason that it does compile is strange. And further it will
produce a runtime error.


As I mentioned, the problem is, that the code compiled and did run ok on
VS6, it was a compile error on VS7 and VS7.1. In VS8 it compiles again
but produces a runtime error.

Best regards
Dirk
ms@nogga.de

2006-02-13, 7:05 pm

Hello,

thanks, I didn't knew where to find the public accessible bug tracking
system. I have found the issue in the system under the BugID FDBK44450

Simply search for auto_ptr.

http://lab.msdn.microsoft.com/produ...55-5c69b7b70821

The bug was opened on the 22.01.2006. So around the same day when I
first encountered this issue also ;-)

Thanks for the info
Dirk


Carl Daniel [VC++ MVP] schrieb:
> ms@nogga.de wrote:
>
> I'd suggest that you open a bug report:
>
> http://lab.msdn.microsoft.com/productfeedback
>
> If you do so, please post a link to the bug report here so that others
> following this thread can add their votes to it.
>
> -cd
>
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com