Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Template compilation, virtual Destructor, and operator new bug???

2 views
Skip to first unread message

Jeffrey Walton

unread,
Sep 8, 2000, 6:07:10 PM9/8/00
to
Hello All,

I'm having problems with an assert in MSVC++, and a custom Vector Class
using a virtual Destructor.

The Template Class Constructor looks as follows:

// vector.h
template <class Object>
class vector {
public:

// Using Default ctor, we clearly malloc for Object[0]
vector( int theSize = 0 ) : currentSize( theSize )
{ objects = new Object[ currentSize ]; }

// virtual and non virtual does not matter
virtual ~vector( ) { delete [ ] objects; }
// ... Other Members
}

If the driver looks as follows, all is OK (no VC++ Assert):
int main( ) {
vector<int> f;
f.resize( 20 );
return 0;
}

If the driver looks as follows, VC++ Asserts:
class Foo {
public:
Foo( ) { }
virtual ~Foo( ) { }
};
int main( ) {
vector<Foo> f;
f.resize( 20 );
return 0;
}

VC++ Language reference says both above examples are valid per
http://msdn.microsoft.com/library/devprods/vs6/visualc/vclang/_pluslang_the_
operator_new_function.htm

ANSI also allows this (see
http://www.cygnus.com/misc/wp/dec96pub/expr.html):
5.3.4.8
When the value of the expression in a direct-new-declarator is zero,
the allocation function is called to allocate an array with no ele-
ments. The pointer returned by the new-expression is non-null and
distinct from the pointer to any other object.

Any Ideas?
Jeff
jwa...@nospam.umbc.edu


Doug Harrison [MVP]

unread,
Sep 11, 2000, 12:06:30 AM9/11/00
to

Jeffrey Walton wrote:

ISTR this is a known bug. Use std::vector, which avoids this problem.

--
Doug Harrison [VC++ MVP]
Eluent Software, LLC
http://www.eluent.com
Tools for Visual C++ and Windows

Jeffrey Walton

unread,
Sep 11, 2000, 10:04:19 PM9/11/00
to
Thank you very much Doug. Hopefully ... :)

Jeff

==========================================

"Doug Harrison [MVP]" <d...@mvps.org> wrote in message
news:r2morscukd3jnbnl0...@4ax.com...

0 new messages