infinite loop using string and size_t

This is a discussion on infinite loop using string and size_t within the C++ Programming forums, part of the General Programming Boards category; Hey guys, why does the following get me an infinite loop? Code: for (size_t i = myString.length() - 2; i ...

  1. #1
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197

    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
    Operating system: Fedora 19 (64-Bit) / Linux 3.11.6-200.fc19 / KDE SC 4.11.2
    Compiler: gcc 4.8.2 with Netbeans IDE 7.4

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Damn, how stupid was THAT?

    Thanks.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Fedora 19 (64-Bit) / Linux 3.11.6-200.fc19 / KDE SC 4.11.2
    Compiler: gcc 4.8.2 with Netbeans IDE 7.4

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, 05:01 PM

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