Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > negative number evaluating greater than string.size()

Reply
Thread Tools

negative number evaluating greater than string.size()

 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      02-20-2004
Rolf Magnus wrote:
>> ...
>> In my opinion, using a signed type to store an unsigned quantity is a
>> low-level design error.

>
> In mine, it's a design error to use an unsigned type if you don't have a
> very good reason to do so. That you only want to store non-negative
> values is not such a reason.


Well, looks like we have different opinions on the subject.

> Why is there no unsigned floating point type, just for the case you want
> to store only postive values?


For several reasons. It is much more difficult to provide
implementation-level support for an additional floating-point type than
for an additional integral type in situations when the underlying
hardware does not support it. Additionally, aside from purely conceptual
reasons, there is very little benefit from one extra mantissa (or
exponent) bit in a floating-point type. Under these circumstances, since
most hardware does not provide support for unsigned floating-point
types, it is not reasonable to force these types into language.

BTW, the reasons for domination of signed integral types in certain
arithmetical contexts in C (inherited into C++, like integral promotions
from smaller types) are probably very similar.

--
Best regards,
Andrey Tarasevich

 
Reply With Quote
 
 
 
 
Jorge Rivera
Guest
Posts: n/a
 
      02-21-2004
>
> Suggest you read the recent thread 'time to get rid of unsigned?', started
> on 17/02/04.
>
> john
>
>


I read all the whining about unsigned, and still disagree....

Regardless, what sense does it make to have a string with size < 0?
Furthermore, he should have been using
void something(std::string::size_type)
to start with.

In my mind, this is just a problem of people not reading the standard
documentation.

The least programmers should do is see the header files to understand
what types are being used, not just trust their instincts.


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      02-21-2004

"Jorge Rivera" <(E-Mail Removed)> wrote in message
news:1NxZb.33233$(E-Mail Removed)...
> >
> > Suggest you read the recent thread 'time to get rid of unsigned?',

started
> > on 17/02/04.
> >
> > john
> >
> >

>
> I read all the whining about unsigned, and still disagree....
>
> Regardless, what sense does it make to have a string with size < 0?


That's not the point. Apparaently both Stroustrup and Lakos recomend
avoiding using unsigned for the size of something because of the potential
overflow problems, particularly (in my view) when one unsigned quantity has
to be subtracted from another.

> Furthermore, he should have been using
> void something(std::string::size_type)
> to start with.


Maybe but you cannot know the problem he was working on.

>
> In my mind, this is just a problem of people not reading the standard
> documentation.
>
> The least programmers should do is see the header files to understand
> what types are being used, not just trust their instincts.
>


Can't disagree with that.

john


 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      02-22-2004
Andrey Tarasevich <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> John Harrison wrote:
> >> ...
> >> Changing line 17 to :
> >>
> >> if(number < static_cast<int>(str.size())) { cout << " hello" << endl; }
> >>
> >> solves the problem.
> >>

> >
> > Of course this is what the OP should do, but notice that the solution
> > effectively rules out strings where size() > INT_MAX. So why design for such
> > strings in the first place? Why not have size() return an int?
> > ...

>
> In my opinion, using a signed type to store an unsigned quantity is a
> low-level design error.


What does unsigned gain you? If you use signed and write code with a
bug, you could end up calculating someone's age as -1 years. If you
use unsigned and write the same code with the same bug you end up with
the age as 4294967295 years (assuming 32 bits as an example). Who
cares that one of those numbers happens to be >0? They are both as
wrong as the other. The code needs to be fixed whether age is signed
or unsigned, and once it is fixed, it will work whether age is signed
or unsigned.

--
GJD
 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      02-22-2004
Jorge Rivera <(E-Mail Removed)> wrote in message news:<1NxZb.33233$(E-Mail Removed)>. ..
> >
> > Suggest you read the recent thread 'time to get rid of unsigned?', started
> > on 17/02/04.
> >
> > john
> >
> >

>
> I read all the whining about unsigned, and still disagree....
>
> Regardless, what sense does it make to have a string with size < 0?


You missed the point. With a signed string size type, any code that
ends up with a size < 0 has a bug. If that same code had been written
with an unsigned string size type, it would still have a bug. The
garbage size value would now happen to be > 0, but so what? That
doesn't make the code any more correct. It is the same bug,
manifesting itself in a different way.

--
GJD

> Furthermore, he should have been using
> void something(std::string::size_type)
> to start with.
>
> In my mind, this is just a problem of people not reading the standard
> documentation.
>
> The least programmers should do is see the header files to understand
> what types are being used, not just trust their instincts.

 
Reply With Quote
 
Jorge Rivera
Guest
Posts: n/a
 
      02-22-2004
>
> You missed the point. With a signed string size type, any code that
> ends up with a size < 0 has a bug. If that same code had been written
> with an unsigned string size type, it would still have a bug. The
> garbage size value would now happen to be > 0, but so what? That
> doesn't make the code any more correct. It is the same bug,
> manifesting itself in a different way.
>


The point is simple, read the documentation, read the correct behavior
of std::string, use it appropriately.

std::string::size_type std::string::size()

can not return a negative number. If anything, functions within
std::string will return std::string::npos, and I doubt there is any
situtation in which std::size() would return it.

Any other assumptions about values returned by string are just that,
assumptions. The behavior of the class is what it is, and we all should
just play by its rules, not whine weahter the implementation is signed
or unsigned.

JLR

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
count the number of element in an array that are greater than somevalues? Peng Yu Perl Misc 3 06-19-2010 09:26 AM
Include header files in quotes or less-than greater-than signs? Dwight Army of Champions C++ 4 03-17-2010 06:39 PM
regex problem: 'greater than' 'less than' and 'equals' not matching! falcon Java 10 02-24-2006 01:23 PM
Random number if range is greater than RAND_MAX? Martin C++ 9 11-15-2004 01:43 PM
Negative setup and Negative hold prem_eda VHDL 5 10-11-2004 12:14 PM



Advertisments