# Can anyone see what's wrong with this code?

This topic is 4879 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
The problem is that arrays are evil.

##### Share on other sites
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

##### Share on other sites
Of course, silly me :)

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

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 13
• 9
• 9
• 15
• ### Forum Statistics

• Total Topics
634077
• Total Posts
3015359
×