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 36 2912
Song Yun Zhao wrote: Hi,
Just wondering what are the dis/advantages of using uint vs int.
What's uint?
Jonathan
Jonathan Turkanis wrote: What's uint?
Jonathan
unsigned int
Song Yun Zhao wrote: Jonathan Turkanis wrote:
What's uint?
unsigned int
Or it could be:
typedef mounatin_range< uinta> uint;
Jonathan
Jonathan Turkanis wrote: Or it could be:
typedef mounatin_range< uinta> uint;
Jonathan
what's ur point?
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
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
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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.?
|
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;
|
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
|
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...
| |
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);
|
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
|
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
|
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?
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
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...
| |