943,015 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 208
  • C++ RSS
Sep 2nd, 2010
0

string::npos = -1?

Expand Post »
Just a quick question, if string::npos represents the largest possible value for a string object, how can this value be -1 if it's an unsigned integer?
I know this value will change for different compilers, as string::npos on my compiler is set to 4,294,967,295 which makes sense but I don't understand how -1 can represent the largest value.

Thanks in advance
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
maceron is offline Offline
4 posts
since Sep 2010
Sep 2nd, 2010
0
Re: string::npos = -1?
When you overflow or underflow an unsigned type, wrap-around occurs. Due to unsigned wrap-around, -1 becomes the largest unsigned value for the type. One common trick is taking advantage of that effect for decrementing loops on an unsigned counter without having to know the actual range of the type:
C++ Syntax (Toggle Plain Text)
  1. for (std::size_t i = n; i < (size_t)-1; i--) {
  2. // ...
  3. }
Last edited by Narue; Sep 2nd, 2010 at 9:59 am.
Administrator
Reputation Points: 6442
Solved Threads: 1391
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 2nd, 2010
0
Re: string::npos = -1?
Because it is an unsigned integer, shouldn't it only be able to store positive values, like when it wraps around wouldn't the value now be 0?
Or is the -1 just like a catch all method, so that nothing else can possibly have that value?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
maceron is offline Offline
4 posts
since Sep 2010
Sep 2nd, 2010
0
Re: string::npos = -1?
>Because it is an unsigned integer, shouldn't it only be able to store positive values
Yes, that's it exactly.

>Or is the -1 just like a catch all method, so that nothing else can possibly have that value?
If it helps, you can think of -1 in unsigned context as (0U - 1) . The effect is the same: subtracting 1 from 0 underflows and wraps around to the largest value for unsigned int.
Last edited by Narue; Sep 2nd, 2010 at 10:34 am.
Administrator
Reputation Points: 6442
Solved Threads: 1391
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 2nd, 2010
0
Re: string::npos = -1?
I think I understand...
So is the -1 used to represent the maximum value for any compiler, as they will be different?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
maceron is offline Offline
4 posts
since Sep 2010
Sep 2nd, 2010
0
Re: string::npos = -1?
-1 in an unsigned context is portable, yes.
Administrator
Reputation Points: 6442
Solved Threads: 1391
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 2nd, 2010
0
Re: string::npos = -1?
Ok thanks for your help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
maceron is offline Offline
4 posts
since Sep 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ Library Training Needed
Next Thread in C++ Forum Timeline: characters should appear on mouse focus :(





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC