0

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?

Community
  • 1
  • 1
Destructor
  • 14,123
  • 11
  • 61
  • 126
  • @downvoters:why downvotes? What's wrong in question? – Destructor Aug 01 '15 at 14:45
  • 2
    Why wouldn't be implementation defined? Why should all implementations have the same exact cout/cin implementation? Why should these objects be the same size when even the size of basic types like `long` are implementation defined? – Ross Ridge Aug 01 '15 at 14:57
  • 4
    I don't see any point to your question. Why should the size be the same on all implementations? What possible benefits could that bring? And you already have a partial answer right there in your question. You already understand that basic types have implementation-specific sizes. But classes are made up of those basic types. (I didn't vote, by the way, but I do think the question doesn't really make sense.) –  Aug 01 '15 at 14:58
  • @hvd: you says "What possible benefits could that bring?". Tell that to James gosling(Java Creator) & anders hejlsberg(C# creator). Don't you think portability is the big advantage of having same size on all implementation? Then why it isn't implementation defined in Java & C#? – Destructor Nov 30 '15 at 15:35
  • @PravasiMeet Java and C# certainly don't guarantee the same size for library classes across different versions of the standard library, and the size does vary across versions. It's just that since Java and C# use references to classes (which are closer to C++ pointers than to C++ references) a lot more than C++ does, so there is a lot less code that ends up using the actual size of the class. –  Nov 30 '15 at 16:08

1 Answers1

3

Because the different people who developed the different compilers decided to implement it in different ways.

Perhaps the differences could be due to choices of optimization or influenced by the underlying ways the platforms work. But it's hard to say more without doing an in depth analysis of the implementations themselves and the platforms they run on.

If you really want to know more, I think you'll have to actually dig into those implementations yourself and start asking about the specifics of them.

TheUndeadFish
  • 8,058
  • 1
  • 23
  • 17
  • 3
    I would have actually been hugely surprised if something with as many layers as `` were implemented (almost) the same way in 2 completely independent implementations. The chances are very very very minimal. – AliciaBytes Aug 01 '15 at 14:57