strange Array behaviour

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 class Bird
{
public:
	Bird():age(1){
	}
	void print()
	{
		cout<<age<<"\n";
	}
private:
	int age;
};

class peng : public Bird
{
public:
	peng():maxspeed(100)
	{
	}
	void print()
	{
		cout<<maxspeed<<"\n";
	}
private:
	int maxspeed;
};

int main()
{
	const int size =3;
	Bird *array1 = new peng[size];

	array1[0].print();
	array1[1].print();  // why it will print 100???
	array1[2].print();

	delete [] array1;

return 0;
}


Question :- why array1[2].print() called extended class print and other called base class print.
@Peter87

but when we create array of object in that case default ctor is being called for each element in array. so we are initializing the values..
I wasn't reading the code properly so I removed my previous answer.
@Peter87

any idea why it is showing strange behaviour?
you created an array of three penguins, then you reinterpreted it as an array of birds.
array1[0] points to the beginning of the first element of the array, and pretends that it's a bird, so birds's member function print reads the first member variable in the object ( that's 1 ) and prints that

array1[1] takes the pointer to the first penguin and increments it by the size of bird, and then it interprets the bytes it finds there as a bird. It so happened in your case that it hit exactly the second member variable (speed) or the FIRST penguin. Bird's function print, thinking it's reading age, read the bytes that constitute the first penguin's speed and printed them as age

array[2] increments the pointer by two bird sizes, which happens to hit exactly the beginning of your Second penguin, so it prints the second penguin's age
+1 Cubbi.

A graphical interpretation of his explanation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15


       sizeof(peng)
|==========================|

peng[0]                     peng[1]                     peng[2]
|                           |                           |
v                           v                           v
[     age    ][  maxspeed  ][     age    ][  maxspeed  ][     age    ][  maxspeed  ]
^             ^             ^
|             |             |
Bird[0]       Bird[1]       Bird[2]

|============|
 sizeof(Bird)
@cubbi @ Disch

I got that thanks.
But i got one more doubt when i write Bird *array1 = new peng[size];. This means that array will store pointer to Bird objet.

than array1[0].print(); worked instead of
array1[0]->print();.

if Array is not storing pointer to Birds then how we can do that?
There's one more issue.

At line 6 - print is not declared virtual. I believe the OP is expecting peng::print to be executed at lines 33-35.

when i write Bird *array1 = new peng[size];. This means that array will store pointer to Bird objet.

No, this means the nameless array created by new[] will store three whole penguin objects, no pointers are stored in it (an array of size pointers to penguins could be created with new peng*[size];). And then it stores the address of the first element of the array in the pointer object "array1", which happens to have the wrong type.
Last edited on
thanks cubbi..
Topic archived. No new replies allowed.