Thread: infinite loop using string and size_t

  1. #1
    spaghetticode
    Guest

    infinite loop using string and size_t

    Hey guys,

    why does the following get me an infinite loop?

    Code:
    for (size_t i = myString.length() - 2; i >= 0; --i) {
        cout << "test" << endl;
    }
    size_t should be able to take the value of 0, so what seems to be the problem here?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    size_t is unsigned, so >= 0 is always TRUE.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    spaghetticode
    Guest
    Damn, how stupid was THAT?

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-14-2011, 11:33 PM
  2. Infinite Loop?
    By Freshjunior in forum C++ Programming
    Replies: 2
    Last Post: 05-14-2010, 06:55 AM
  3. infinite loop
    By liukinhei in forum C Programming
    Replies: 3
    Last Post: 03-24-2008, 11:01 PM
  4. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM
  5. almost infinite loop
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2002, 06:01 PM