when to use uint? dis/advantages?

S

Song Yun Zhao

Hi,

Just wondering what are the dis/advantages of using uint vs int. When
would be the best time to use it?

Personally I don't use uint that much, but I like to optimize my code
and make it as effective as possible. So I feel that using an int where
only an uint is needed is a waste.

e.g. something like (int i = 0; i < 100; i++)
could probably better be written as (uint i = 0; i < 100; i++)
or maybe even better (unsigned short i = 0; i < 100; i++)

One big advantage of using uint is that if I have a function like

void foobar(uint i)
{
if (i >= 0) {
cout << "generate a warning, this is always true" << endl;
}
}

and calling foobar(-1) will generate a warning. This can help me iron
out some problems/bugs at compile time.

However using uint does require more work, for example when comparing an
uint to int will generate a warning, hence would require a static_cast.

Anyway, I want to know what other programmers are doing. I use Qt a lot
at work, and I don't see a lot of uint in Qt's code. Could this be for
portability reasons?

Anyway all comments are appricated. TIA.

Song
 
R

rajkumar

Hey

Lots of functions return 0 - N for okay and -1 for error conditions.
If you store these values in a unsigned int you will never be able to
detect these error conditions.

Also if somebody refactors you code later and changes

for ( unsigned int i = 0; u < 100 ; i++ )

to

for ( unsigned int i = 100 -1 ; i >= 0 ; --i )

you get an infinite loop


Raj
 
I

Ioannis Vranos

Song said:
Hi,

Just wondering what are the dis/advantages of using uint vs int. When
would be the best time to use it?


In technical discussions we strive for accuracy. uint does not mean
anything in particular.

Personally I don't use uint that much, but I like to optimize my code
and make it as effective as possible. So I feel that using an int where
only an uint is needed is a waste.

e.g. something like (int i = 0; i < 100; i++)
could probably better be written as (uint i = 0; i < 100; i++)
or maybe even better (unsigned short i = 0; i < 100; i++)

One big advantage of using uint is that if I have a function like

void foobar(uint i)
{
if (i >= 0) {
cout << "generate a warning, this is always true" << endl;
}
}

and calling foobar(-1) will generate a warning. This can help me iron
out some problems/bugs at compile time.


assigning -1 to an unsigned integral type variable, assigns the maximum
value of its type to it.


However using uint does require more work, for example when comparing an
uint to int will generate a warning, hence would require a static_cast.

Anyway, I want to know what other programmers are doing. I use Qt a lot
at work, and I don't see a lot of uint in Qt's code. Could this be for
portability reasons?

Anyway all comments are appricated. TIA.



Use signed integral types when you need to use them for negative values
too. If you deal only with positive values and/or want to have more
positive value range at the same type size, use the unsigned types.
 
S

shabbir

uint and int will not make a slight of difference in todays speed of
computer and its almost obsolete.

it is there in C++ for a simple reason "Backward compatibility" and
will not make any help to you as far as my shallow knowledge goes.
 
E

eed132

I don't know of any reason why it would make a speed difference, but it
is not there just for backwards compatibility by any stretch of
imagination.

Remember that the types are different sizes. Have data that can range
up to, say, 4 billion but you can gurantee will be positive? You gotta
use an unsigned int.
 
H

Howard

Song Yun Zhao said:
Hi,

Just wondering what are the dis/advantages of using uint vs int. When
would be the best time to use it?

There is no such type as uint. You are free to typedef unsigned int as uint
(or your compiler vendor may have done so for you, in some header), but
otherwise it means nothing.

By the way, if you're just saying "uint" because it takes too long to write
"unsigned int", just like I see you writing "u r" instead of "you are", then
please refrain from doing that. We really need to stay technically accurate
here (as much as possible, anyway). Thanks.
Personally I don't use uint that much, but I like to optimize my code and
make it as effective as possible. So I feel that using an int where only
an uint is needed is a waste.

What is "wasted"?
e.g. something like (int i = 0; i < 100; i++)
could probably better be written as (uint i = 0; i < 100; i++)
or maybe even better (unsigned short i = 0; i < 100; i++)

Or even unsigned char! Your compiler may actually use whatever size is
appropriate to your platform, provided it behaves the same. Using unsigned
int just takes a little longer to write than int. It is not likely to
effect your performance at all.
One big advantage of using uint is that if I have a function like

void foobar(uint i)
{
if (i >= 0) {
cout << "generate a warning, this is always true" << endl;
}
}

and calling foobar(-1) will generate a warning. This can help me iron out
some problems/bugs at compile time.

Calling foobar(-1) will change the -1 to an unsigned int (assuming that uint
is the same as unsigned int), and will result in a very large positive
integer, which is obviously greater than 0. Also, the code above will issue
a warning no matter what you pass as the parameter, since an unsigned int is
_always_ zero or greater. I guess I'm just not sure I understand what your
point was. Do you actually have the code above somewhere?
However using uint does require more work, for example when comparing an
uint to int will generate a warning, hence would require a static_cast.

If you're comparing against a (signed) int, then you ought to use a (signed)
int to compare against, unless you can guarantee that the (signed) int will
never actually be negative. Using a static_cast is not a bad thing, if you
know that that's what you have to do. And this type of comparison is one of
the few places I'd use it. (Unless of course I could avoid it by using
matching types in the first place.)
Anyway, I want to know what other programmers are doing. I use Qt a lot at
work, and I don't see a lot of uint in Qt's code. Could this be for
portability reasons?

I use unsigned int (or char, or short, or long) where I think it's
appropriate. They pay me to think here, so I do. :)

Seriously, if I know that the value in question will never be negative, I'm
likely to use unsigned, since it becomes apparent to those reading my code
that the value is intended to be positive or zero (and will always be so).
If the value _may_ be negative for any reason, then I _must_ use a signed
version. I'll admit that I sometimes write simple for loops without the
"unsigned" specifier, but that's really just laziness on my part, not a
conscious choice to use signed. (Oh, and sometimes I change my mind and add
or remove the unsigned specifier, as I develop my code and realize the other
parts I'm interacting with differ from my previous assumptions.)

One other point: I don't actually use int (or unsigned int) that much.
Mostly I use short, long or char (or the unsigned versions thereof). Those
give me more of an obvious indication of the range of values my variable is
going to take.

-Howard
 
J

Julie

Hey

Lots of functions return 0 - N for okay and -1 for error conditions.

Indication of poor design: using a value to indicate both a value and an error
condition, should be separate variables or an exception.
If you store these values in a unsigned int you will never be able to
detect these error conditions.

Also if somebody refactors you code later and changes

for ( unsigned int i = 0; u < 100 ; i++ )

to

for ( unsigned int i = 100 -1 ; i >= 0 ; --i )

you get an infinite loop

And who ever did the refactoring and checked that code into the repository
should be reprimanded/re-educated.
 
J

Julie

Song said:
Hi,

Just wondering what are the dis/advantages of using uint vs int. When
would be the best time to use it?

Use the type that is most suitable for your domain.

If the domain is non-negative integers less than UINT_MAX, use unsigned int.

If the doamin is integers in the range of INT_MIN and INT_MAX, use (signed) int.

etc.
 
R

red floyd

Song said:
not really, r u trying to say that uint is not a standard type?

Song

Yes, that's exactly what he's trying to say. Since uint is not a
standard type, the usage depends on the application's definition of uint.
 
R

rajkumar

unless you want to redesign / refactor libraries you have to live with
it.
And who ever did the refactoring and checked that code into the repository
should be reprimanded/re-educated.

The idea here was that its more error prone... unless everybody else
(fellow developers) feels the same about int / unsigned int
it would be a bad idea

raj
 
T

Thomas Matthews

Song said:
Hi,

Just wondering what are the dis/advantages of using uint vs int. When
would be the best time to use it?
Generally, uint is often followed by the number of bits. This is
often found in embedded systems, where the bit-width of a variable
is important. For example:
#define UINT8 unsigned char
#define UINT16 unsigned short
#define UINT32 unsigned int
This is primarily used to make porting to other platforms easier,
but that is a whole separate and lengthy topic.
Anyway all comments are appricated. TIA.
You should search the newsgroup. There have been many heated
discussions about signed versus unsigned integers and when
to use them. One person suggested that the unsigned integer
type be removed from the language.

My own personal belief is that unsigned should be used for
ordinal types, where the value is never negative, such as
counting fruit. Unsigned is also used when all the bits
of an integer are used. Signed integers are used when
the value can go negative, such as temperatures in Fahrenheit.

For small quantities or values, your best bet is to spend
time doing something else than worrying about this issue.
For example, on most platforms, the number 4
can be easily handled in the native integral datatype; so
the point of using signed versus unsigned is moot.
 
O

Owen Jacobson

not really, r u trying to say that uint is not a standard type?

Exactly.

Many platforms provide a 'uint' type that is either an alias or a typedef
from 'unsigned int', but this is in no way guaranteed by the C++ standard.
I don't think anyone here honestly didn't understand you, but in the
future you should use the standard type.
 
S

Song Yun Zhao

Howard said:
"unsigned int", just like I see you writing "u r" instead of "you are", then
please refrain from doing that. We really need to stay technically accurate
here (as much as possible, anyway). Thanks.

no worries, I'll stick to the standard next time. thanks.
What is "wasted"?

an unsigned int could be better used to represent a bigger range of
positive integers (obviously 0-2^32, assuming a 32 bit platform),
whereas an int can only represent up to 2^31 positive integers. So if I
know that a number is always going to be a positive integer, I feel like
I'm wasting some "range". The same reason you would use a char instead
of a short int etc.

Calling foobar(-1) will change the -1 to an unsigned int (assuming that uint
is the same as unsigned int), and will result in a very large positive
integer, which is obviously greater than 0. Also, the code above will issue
a warning no matter what you pass as the parameter, since an unsigned int is

agreed, therefore my compiler (GNU g++) gives me *two* warnings, which
serves as an indication that I'm doing something not right.
I use unsigned int (or char, or short, or long) where I think it's
appropriate. They pay me to think here, so I do. :)

I probably speak less than 10 words (hi, morning, g'day, well you get
the idea) to ppl at work each day, guess what I do the rest of the time?
;) Probably doesn't do much good to my communication/social skills, but
hey I'm a programmer. :(

cheers
 
I

Ioannis Vranos

Howard said:
Using unsigned
int just takes a little longer to write than int. It is not likely to
effect your performance at all.


He can just type "unsigned" instead of "unsigned int". :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top