Before you people start to mark this as duplicate, I've already read this. But it doesn't answer my question.
I agree that sizeof(int) is implementation defined behavior in C & C++. But I got surprised when I tested following program on various compilers.
#include <iostream>
using std::cout;
int main()
{
cout << "Sizeof cout object: " << sizeof(cout) << '\n';
cout << "Sizeof cin object: " << sizeof(std::cin) << '\n';
}
Output on g++ 4.8.1 & 4.9.2
Sizeof cout object: 140
Sizeof cin object: 144
Output on Visual Studio 2010
Sizeof cout object: 80
Sizeof cin object: 88
Output on online compiler (See live demo here)
Sizeof cout object: 272
Sizeof cin object: 280
My question is why it is implementation defined behavior? Why not having same size on all implementations of C++? Wouldn't it be if they have same size on all compilers? My friend told me that this decision is taken due to hardware & performance reasons. It would be very helpful If you explain the answer in depth at lowest level. How hardware plays role here? How performance can be gained by having different sized types on different implementations? Why there is so much difference in size for of cin & cout objects in the above program on various compilers?