Can anyone see what's wrong with this code?

Started by
2 comments, last by OklyDokly 18 years, 7 months ago
Hey I thought I knew C++ pretty well, but this code seems to be crashing on the 3rd line of the main function and I can't see why. Anyone got any ideas?

// Me class
class Me
{
protected:
	int m_I;
public:
	Me( int i ) { m_I = i; }
	virtual ~Me() {}

	virtual void Print() = 0;
};


// Inherited you class
class You : public Me
{
	int j;
public:
	You() :	 Me( 0 )
	{
		j = m_I;
	}

	virtual ~You() {}

	virtual void Print()
	{
		printf( "%i\n", j );
	}
};



int _tmain(int argc, _TCHAR* argv[])
{
	Me* you = new You[ 3 ];
	you[0].Print();
	you[1].Print();
	you[2].Print();

	return 0;
}

Thanks
Advertisement
The problem is that arrays are evil.
Arrays cannot be treated polymorphicly. In a Type * array the elements have static type Type and an index of the form array[index] is calculated equivalently to *static_cast< Type * >(static_cast< char * >(array) + index * sizeof(Type)). The expression new DerivedType[size] creates an array with members whose type is DerivedType. Elements in this array are located equivalently to *static_cast< DerivedType * >(static_cast< char * >(array) + index * sizeof(DerivedType)). Since sizeof(Type) usually isn't the same as sizeof(DerivedType) the indices do not match up. That might be a little heavy for you, so here's a diagram:
  sizeof(Type)    <------>---+--------+--------+--------+--------+---   |        |        |        |        |---+--------+--------+--------+--------+---       ^        ^        ^        ^    array[0] array[1] array[2] array[3] sizeof(DerivedType)    <---------->---+------------+------------+------------+------------+---   |            |            |            |            |---+------------+------------+------------+------------+---

So the array is accessed as if it were laid out in the top diagram, but in reality it is laid out as in the bottom diagram. The solution is to use an array of pointers to objects:
int _tmain(int argc, _TCHAR* argv[]){	Me * * you = new You*[ 3 ];	you[0] = new You();	you[1] = new You();	you[2] = new You();	you[0]->Print();	you[1]->Print();	you[2]->Print();	return 0;}

(Of course, this creates a memory pool since nothing gets deleted and is not exception safe, but it's only a trivial example).

Enigma
Of course, silly me :)

Thanks for your help though, annoying how the fix is always a little more complicated :S...

This topic is closed to new replies.

Advertisement