sizeof(cin) and sizeof(cout)

This is a discussion on sizeof(cin) and sizeof(cout) within the C++ Programming forums, part of the General Programming Boards category; Why sizeof(cin) is 144 and sizeof(cout) is 140 always even after cin.clear() after cin.ignore() Its the Same Why ?? Can ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    219

    sizeof(cin) and sizeof(cout)

    Why sizeof(cin) is 144
    and sizeof(cout) is 140
    always
    even after
    cin.clear()
    after cin.ignore() Its the Same
    Why ??
    Can anyone explain Me ??

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    sizeof has nothing to do with the number of characters in the buffers. What are you trying to do?

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    But Why its 144 and 140 ??
    Whats teh Magic in these 2 Numbers.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    No magic, just implementation detail.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    sizeof returns the number of bytes of memory an object (or type) occupies. So the cin object occupies 144 bytes of space and the cout object takes up 140 bytes of space. It might be different with a different library implementation.

    The number depends on how the library implementors created the classes that cin and cout are objects of. I believe cin is a basic_istream and cout is a basic_ostream or something like that. Apparently on your platform those classes use that much space for their data.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Thanks got the Idea

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    But after cin.clear()
    and cin.ignore(1000)
    the buffer should get cleared compleately
    But even after cin.clear() and cin.ignore I am seeing the same result.
    Why ??

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Like I said in the very first post, sizeof has absolutely nothing to do with buffers. They are completely unrelated.

    sizeof is a compile time lookup based on the size of the class. It will never change for those two objects on your platform nobody what code you write.

    What are you trying to do? Are you trying to find out how many characters are in the buffer?

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Ah!
    Ok.
    Yes I am trying to Calculate the Size of the cin Buffer.So how can I do it ??

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    If I'm understanding your question ....
    Code:
       std::cin.rdbuf()->in_avail();
    will yield the value you seek.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I believe that might not be reliable. I'm not sure there's a portable way to calculate how much data is in the buffer.

    For cin, you probably want to read in a line up to a newline. User input is generally terminated by the <enter> key, so you will read in everything the user typed (in most cases). Then you can use that string as your own buffer.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Well, you can use the streambuf interface to find out how much data can be buffered. You cannot really use it to find out how much is buffered, because it may not be the only buffer.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21