By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,971 Members | 890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,971 IT Pros & Developers. It's quick & easy.

why always out_of_range using size_type?

P: n/a
Hi,guys!
Please look at this problem, when I use "int i", it success, but when I
use string::size_type i, it fail with a out_of_range exception.
//---------------- begin----------------------------

int str_to_num(const string& s)
{
int num = 0;
int interval = 1;
int i; // success
//string::size_type i; //always cause a out_of_range exception

for (i = s.length() - 1; i >= 0; --i) {
num += static_cast<int>(s.at(i) - 'a' + 1) * interval;
interval *= 26;
}

return num;
}

//-----------------end------------------------------
//a b c .... z aa ab ac .... az ba bb bc .... yz za zb zc .... zz aaa aab
aac ..
//.. change these string to number:
//1 2 3 .... 26 27 28 29 .... 52 53 54 55 .... 676 677 678 679 .... 702 703
704
//705 ....

IDE/Compiler: Borland C++ Builder 6

I know that the string::at() function cause this out_of_range exception, but
why size_type cause this?

Any help is appreciated, thanks!
--
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Frederick Ding wrote:
Hi,guys!
Please look at this problem, when I use "int i", it success, but when I
use string::size_type i, it fail with a out_of_range exception.
//---------------- begin----------------------------

int str_to_num(const string& s)
{
int num = 0;
int interval = 1;
int i; // success
//string::size_type i; //always cause a out_of_range exception

for (i = s.length() - 1; i >= 0; --i) {
num += static_cast<int>(s.at(i) - 'a' + 1) * interval;
interval *= 26;
}

return num;
}


I guess that size_type is an unsigned type. Values of unsigned types can
never be smaller than zero, so the condition in the for loop is always
true, and the index runs out of bounds. Concrete: if i is zero at one
point, you do another loop, which decrements i. Since i is unsigned, it
wraps around to some large positive number, which is most probably out
of range.

General rule: Be careful when comparing for >= 0, and never do it for
unsigned types.

Oliver

--
Email: kr*****@cs.ucdavis.edu
WWW: http://graphics.cs.ucdavis.edu/~okreylos/ResDev
Jul 23 '05 #2

P: n/a
> Please look at this problem, when I use "int i", it success, but
when I
use string::size_type i, it fail with a out_of_range exception.
string::size_type is usually an unsigned integer.
int i; // success
//string::size_type i; //always cause a out_of_range exception for (i = s.length() - 1; i >= 0; --i) {


If 'i' is unsigned, this will always be true. When 'i' is 0 and you
decrement it, it becomes the largest integer that may be represented.

Jonathan

Jul 23 '05 #3

P: n/a
> If 'i' is unsigned, this will always be true. When 'i' is 0 and you
decrement it, it becomes the largest integer that may be represented.


The bottom line: raise the warning level of your compiler to the highest
possible, and then review the warnings. If you do that, you will probably
find a warning that the "condition is always true"...

It is never a good thing to leave unexplained warnings in your code, or to
lower the warning level to get rid of them. Treat warnings as errors unless
there is a good reason not to do so.

Rade
Jul 23 '05 #4

P: n/a
Jonathan Mcdougall wrote:
Please look at this problem, when I use "int i", it success, but


when I
use string::size_type i, it fail with a out_of_range exception.

string::size_type is usually an unsigned integer.

It is always going to be some unsigned integral type.
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.