Thread: cin memory leak?

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    cin memory leak?

    I'm using a program (altnew.cc) that was provided by my class to measure the memory allocated. It's been working fine and I haven't been leaking any memory until I started changing how I read in input.

    Code:
    string input;
    cin >> input;
    I typed in a 'q' which exits my program and tells me the bytes allocated at the end. It should be 0 but theres 14 bytes allocated after cin which don't go away. Do I have to flush cin or something?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by jcafaro10 View Post
    I'm using a program (altnew.cc) that was provided by my class to measure the memory allocated. It's been working fine and I haven't been leaking any memory until I started changing how I read in input.

    Code:
    string input;
    cin >> input;
    I typed in a 'q' which exits my program and tells me the bytes allocated at the end. It should be 0 but theres 14 bytes allocated after cin which don't go away. Do I have to flush cin or something?
    Probably your C++ runtime allocates some global data during setup of the standard streams. The runtime should theoretically free this data when it destructs itself, but, well... welcome to reality.

    Depending how you look at it, it's either a bug in the iostreams library, or it's harmless. I've seen this sort of thing so often I don't stop to worry about it.

    EDIT: I suppose it could be a bug in this altnew.cc module where it fails to account for some weird thing the iostreams library is doing. I'd give it 50/50% either way.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    cin cannot possibly deallocate its internally used data before any library-driven memory reporter runs. If the memory reporter can run, any other code can run too, and it might use cin. Bad if cin were to shut down before that.
    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

Similar Threads

  1. Detecting memory leak in complicating source code
    By mosxopul in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2009, 11:41 AM
  2. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 06:50 AM
  3. memory leak
    By rahulsk1947 in forum C Programming
    Replies: 2
    Last Post: 11-11-2007, 01:27 PM
  4. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM