Thread: infinite loop using string and size_t

  1. #1
    casual coder spaghetticode's Avatar
    Join Date
    Jan 2011
    Location
    Hesse, Germany
    Posts
    259

    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?
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8 | Acer Aspire V3-771G
    OS: Windows 10 Home

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    35,954
    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
    casual coder spaghetticode's Avatar
    Join Date
    Jan 2011
    Location
    Hesse, Germany
    Posts
    259
    Damn, how stupid was THAT?

    Thanks.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8 | Acer Aspire V3-771G
    OS: Windows 10 Home

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