473,804 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

when to use uint? dis/advantages?

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
Jul 23 '05 #1
36 2912
Song Yun Zhao wrote:
Hi,

Just wondering what are the dis/advantages of using uint vs int.


What's uint?

Jonathan
Jul 23 '05 #2
Jonathan Turkanis wrote:
What's uint?

Jonathan

unsigned int
Jul 23 '05 #3
Song Yun Zhao wrote:
Jonathan Turkanis wrote:
What's uint?
unsigned int


Or it could be:

typedef mounatin_range< uinta> uint;

Jonathan
Jul 23 '05 #4
Jonathan Turkanis wrote:

Or it could be:

typedef mounatin_range< uinta> uint;

Jonathan


what's ur point?
Jul 23 '05 #5
Song Yun Zhao wrote:
Jonathan Turkanis wrote:

Or it could be:

typedef mounatin_range< uinta> uint;

Jonathan


what's ur point?


I meant to say mountain_range. Now is everything clear?

Jonathan
Jul 23 '05 #6
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

Jul 23 '05 #7
Jonathan Turkanis wrote:

I meant to say mountain_range. Now is everything clear?


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

Song
Jul 23 '05 #8
Song Yun Zhao wrote:
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.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #9
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.

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3853
by: Ender | last post by:
Let's assume you are developing a database application and you have an ID field. You setup the ID field in the database as a 1 up increment starting at zero. Whenever I see programs, everyone always uses the int field to represent the userID. Should this really be a uint since the field will never be negative, only positive? Is there a performance penalty using the uint type? Furthermore, let's say I am building an application...
1
2045
by: ±èÀçȲ | last post by:
//this code generates the error. uint a=1,b=2; Console.WriteLine(a << b); Console.WriteLine(a >> b); What problem does "uint type" have.?
9
2459
by: Bjorn Abelli | last post by:
Hi all, When I run a normal application I can get who started a process with the following example: Process myProcesses = Process.GetProcesses(); foreach(Process p in myProcesses) { string user = p.StartInfo.EnvironmentVariables;
4
3423
by: TT (Tom Tempelaere) | last post by:
Hi people I have a library built using MSVC6 which exports a method with the following signature void library_method( UINT* sw_mode ) I am trying to use PInvoke to be able to call it from my C# project, but I cannot find a suitable marshal type Is there a type that I can use to marshal a UINT*? If not, are there any other integrated solutions to this problem
1
4463
by: genojoe | last post by:
I have converted the code shown below from C#. My problem involves the uint data type. When converted to VB.NET it becomes: Uint32 and causes two compile errors. The error message in VB.NET involves not being able to convert between Uint32 and Integer. The error messages occur in the two Dim statements in the VB.NET GetRtfImage() method. I can make the code compile by changing Uint32 to Int32 but will this conversion make the dll not...
0
1182
by: NvrBst | last post by:
I want to be able to tell when the mouse left clicks even when my application doesn't have focus. I tried adding this to my form ------------------------------------------ protected override void WndProc(ref Message m) { if(m.Msg == WM_NCLBUTTONDOWN) { System.Windows.Forms.MessageBox.Show("Mouse Clicked"); } base.WndProc(ref m);
4
1993
by: Jure Bogataj | last post by:
Hello! Why I cannot apply this operator to UINT or INT type in c# (VS 2005)? uint MyConst; uint SomeValue; .... MyConst = MyConst & (!SomeValue) Trying to erase certain bit inside MyConst. I come from Delphi world and the
14
3797
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain more why C++/CLI would be better to PInvoke than doing the PInvoke in C#? Because, usually in C# as you already know we use DLLImport and extern
2
2141
by: Tim Sprout | last post by:
The P/Invoke Interop Assistant (http://www.codeplex.com/clrinterop) generates a signature for GetDefaultPrinter using an uint type for pcchBuffer: public static extern bool GetDefaultPrinter( StringBuilder pszBuffer, ref uint pcchBuffer); However, StringBuilder only accepts type int. Is it safe to cast uint to int?
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10352
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10097
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9175
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7642
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.