Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > signed/unsigned..?

Reply
Thread Tools

signed/unsigned..?

 
 
Søren Johansen
Guest
Posts: n/a
 
      12-12-2003
Hi,

in Large Scale c++ Design, John Lakos suggests that you should avoid the
"unsigned" keyword and use regular ints. Now I know that this book is
considered mostly outdated and I do use namespaces (which he also suggests
avoiding) and other things, but this particular topic has me puzzled.

The problem is that stl has a lot of size_t types that are unsigned and
since I run a no-warning policy, this forces me to do a lot of casting if
using pure int's. On the other hand, when you start to have unsigned
variables, I find that you risk getting errors that can be extremely subtle
and hard to track down.

Any advice and experience appreciated.

Søren


 
Reply With Quote
 
 
 
 
Hendrik Belitz
Guest
Posts: n/a
 
      12-12-2003
Søren Johansen wrote:

> Hi,
>
> in Large Scale c++ Design, John Lakos suggests that you should avoid the
> "unsigned" keyword and use regular ints.


I don't agree with this. Especially for range checking purposes the usage of
unsigned values is preferable. Just look at the following example.

int i = ...
if ( i > 0 || i < 1000 ) doWhateverYouLike

using unsigned int this becomes
unsigned int = ...
if ( i < 1000 ) doWhateverYouLike

--
Dipl.-Inform. Hendrik Belitz
Central Laboratory of Electronics
Research Center Juelich
 
Reply With Quote
 
 
 
 
Josephine Schafer
Guest
Posts: n/a
 
      12-12-2003

"Søren Johansen" <nospam@please> wrote in message
news:3fd9a963$0$182$(E-Mail Removed). ..
> Hi,
>
> in Large Scale c++ Design, John Lakos suggests that you should avoid the
> "unsigned" keyword and use regular ints. Now I know that this book is
> considered mostly outdated and I do use namespaces (which he also suggests
> avoiding) and other things, but this particular topic has me puzzled.
>
> The problem is that stl has a lot of size_t types that are unsigned and
> since I run a no-warning policy, this forces me to do a lot of casting if
> using pure int's. On the other hand, when you start to have unsigned
> variables, I find that you risk getting errors that can be extremely subtle
> and hard to track down.
>


I don't know in what context Lakos has made the statement.
But it's not correct to assume size_t is unsigned int or anything else.
Take example of std::string.
#include <string>
int main()
{
std::string Buffer("abc");
std::string::size_type loop = Buffer.length ();
}

If you take Buffer.length() to fit into an unsigned int, the result may/may not
be correct depending on whether
std::string::size_type is actually unsigned int or not. So to be safe always
use the size types provided by the class.

HTH,
J.Schafer

..



 
Reply With Quote
 
lilburne
Guest
Posts: n/a
 
      12-12-2003


Hendrik Belitz wrote:
>
> I don't agree with this. Especially for range checking purposes the usage of
> unsigned values is preferable. Just look at the following example.
>
> int i = ...
> if ( i > 0 || i < 1000 ) doWhateverYouLike
>
> using unsigned int this becomes
> unsigned int = ...
> if ( i < 1000 ) doWhateverYouLike
>


int i = ...

assert(i >= 0);

works just as well and causes an error if the constraint is violated.

 
Reply With Quote
 
Harald Grossauer
Guest
Posts: n/a
 
      12-12-2003
Hendrik Belitz wrote:

> Søren Johansen wrote:
>
>> Hi,
>>
>> in Large Scale c++ Design, John Lakos suggests that you should avoid the
>> "unsigned" keyword and use regular ints.

>
> I don't agree with this. Especially for range checking purposes the usage
> of unsigned values is preferable. Just look at the following example.


That's what I thought once too. But then it took me two days to find the
following bug in my program:

unsigned int i;
/*
....
lots of other code
.....
*/
for( i = nMax; i>0; --i ) {...

 
Reply With Quote
 
Søren Johansen
Guest
Posts: n/a
 
      12-12-2003
> I don't know in what context Lakos has made the statement.
> But it's not correct to assume size_t is unsigned int or anything else.
> Take example of std::string.
> #include <string>
> int main()
> {
> std::string Buffer("abc");
> std::string::size_type loop = Buffer.length ();
> }
>
> If you take Buffer.length() to fit into an unsigned int, the result

may/may not
> be correct depending on whether
> std::string::size_type is actually unsigned int or not. So to be safe

always
> use the size types provided by the class.


I realize this but there are some things that I dislike about it.
Consider this (made up) simple example:

class Store
{
std::vector<int> _somevector;
..other stuff..

public:
std::vector::size_type GetElementCount() { return _somevector.size(); }
void DoSomethingToElement(std::vector::size_type index);
};

Here, it seems to me that having to use the std::vector::size_type reveals
more implementation than I like in the interface. Furthermore, what if you
were to use the same index for two containers of different types?

Søren


 
Reply With Quote
 
Søren Johansen
Guest
Posts: n/a
 
      12-12-2003
> int i = ...
> if ( i > 0 || i < 1000 ) doWhateverYouLike
>
> using unsigned int this becomes
> unsigned int = ...
> if ( i < 1000 ) doWhateverYouLike


Yes but if you only want to make sure i is positive, and i has been assigned
to some unsigned value, you would have to check for i < 2147483647..
Wouldn't you? My point is that it seems that unsigned values don't really
solve the problem that they appear to. Yes, an unsigned value can never be
negative but assigning a signed value to it or comparing with a signed value
produces weird results.
The argument about the one extra bit that is gained seems to me to only be
viable in very few, special cases. Your argument is better but on the other
hand it can produce errors that are hard to identify.

Søren


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      12-12-2003
Harald Grossauer wrote:
>
> Hendrik Belitz wrote:
>
> > Søren Johansen wrote:
> >
> >> Hi,
> >>
> >> in Large Scale c++ Design, John Lakos suggests that you should avoid the
> >> "unsigned" keyword and use regular ints.

> >
> > I don't agree with this. Especially for range checking purposes the usage
> > of unsigned values is preferable. Just look at the following example.

>
> That's what I thought once too. But then it took me two days to find the
> following bug in my program:
>
> unsigned int i;
> /*
> ...
> lots of other code
> ....
> */
> for( i = nMax; i>0; --i ) {...



There is no problem with that. But I know what you mean.
I myself have been bitten lots of times by turning

for( i = 0; i < nMax; ++i )

around (because eg. I wanted to change the direction
in which an array is traversed) to:

for( i = nMax - 1; i >= 0; --i )

if i is unsigned this will no longer work

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      12-12-2003
Harald Grossauer wrote in news:(E-Mail Removed):

> That's what I thought once too. But then it took me two days to find
> the following bug in my program:
>
> unsigned int i;
> /*
> ...
> lots of other code
> ....
> */
> for( i = nMax; i>0; --i ) {...
>
>


There are several other options that could help here:

1) Get a better compiler and switch all warnings on.

2) Recode as:

for( unsigned i = nMax; i>0; --i ) {

3) Don't call unsigned int's i (maybe u).

4) Don't write computer programmes.

Only (4) is guaranted to work.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Cy Edmunds
Guest
Posts: n/a
 
      12-12-2003
"Søren Johansen" <nospam@please> wrote in message
news:3fd9a963$0$182$(E-Mail Removed). ..
> Hi,
>
> in Large Scale c++ Design, John Lakos suggests that you should avoid the
> "unsigned" keyword and use regular ints. Now I know that this book is
> considered mostly outdated and I do use namespaces (which he also suggests
> avoiding) and other things, but this particular topic has me puzzled.
>
> The problem is that stl has a lot of size_t types that are unsigned and
> since I run a no-warning policy, this forces me to do a lot of casting if
> using pure int's. On the other hand, when you start to have unsigned
> variables, I find that you risk getting errors that can be extremely

subtle
> and hard to track down.
>
> Any advice and experience appreciated.
>
> Søren
>
>


Sorry, I have no useful advice. I have the same problem and use casting to
int far more than I would like to.

--
Cy
http://home.rochester.rr.com/cyhome/


 
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




Advertisments