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
    Hesse, Germany
    Posts
    202

    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: Fedora 22 64-bit | Linux 4.1.6-200 | KDE Plasma 5.3.2
    Compiler: gcc 5.1.1 20150618 | KDevelop 4.7.1

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    33,292
    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
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Hesse, Germany
    Posts
    202
    Damn, how stupid was THAT?

    Thanks.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8 | Acer Aspire V3-771G
    OS: Fedora 22 64-bit | Linux 4.1.6-200 | KDE Plasma 5.3.2
    Compiler: gcc 5.1.1 20150618 | KDevelop 4.7.1

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