In the code below, should i be casted to "unsigned int" type first before
passing it into f()? Thanks!
void f(unsigned int);
int main()
{
unsigned short i;
i = 23;
/* Should i be casted to "unsigned int" type first before passing it
into f()? */
f(static_cast<unsigned int>(i))
} 7 1265
* Busin: void f(unsigned int);
int main() { unsigned short i; i = 23; /* Should i be casted to "unsigned int" type first before passing it into f()? */ f(static_cast<unsigned int>(i)) }
No.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alf P. Steinbach posted: * Busin: void f(unsigned int);
int main() { unsigned short i; i = 23; /* Should i be casted to "unsigned int" type first before passing it into f()? */ f(static_cast<unsigned int>(i)) }
No.
Think about that there for a second, why did the OP ask that question? They
want to understand the concept of what's going on. "No." doesn't answer
their question.
-JKop
Busin posted: In the code below, should i be casted to "unsigned int" type first before passing it into f()? Thanks!
void f(unsigned int);
int main() { unsigned short i; i = 23; /* Should i be casted to "unsigned int" type first before passing it into f()? */ f(static_cast<unsigned int>(i)) }
That's what I myself call an implicit conversion, ie. you don't have to
explicitly specify that it is to be converted. Consider:
unsigned char snake = 12;
unsigned long int lizard = 4000000000UL;
snake = lizard;
You may be thinking here, Oh No! There's not enough room in that unsigned
char for that number! You may be tempted to write:
snake = (unsigned char)lizard;
But it's unneccesary. It's done "automatically", "implicitly". The unsigned
long int number is truncated, most likely from 4000000000 to 255, for
storage in the unsigned char.
My suggestion to you is just try to compile your code without any casts. If
it compiles, then all is fine and dandy. If it doesn't, *THEN* you can go
playing with casts.
-JKop
Busin wrote: In the code below, should i be casted to "unsigned int" type first before passing it into f()? Thanks!
void f(unsigned int);
int main() { unsigned short i; i = 23; /* Should i be casted to "unsigned int" type first before passing it into f()? */ f(static_cast<unsigned int>(i)) }
There is no need for a casting, since an implicit conversion of built in
types takes place here.
Regards,
Ioannis Vranos
JKop wrote: unsigned long int lizard = 4000000000UL;
Also the UL thing is not required here (it determines that it is an
unsigned long constant but is not needed here).
It is needed in cases like:
// function overloading
void something(int i);
void something(long i);
void something(unsigned long i);
something(4UL);
That said, your use of it does not raise any concerns.
snake = lizard;
You may be thinking here, Oh No! There's not enough room in that unsigned char for that number! You may be tempted to write:
snake = (unsigned char)lizard;
But it's unneccesary. It's done "automatically", "implicitly". The unsigned long int number is truncated, most likely from 4000000000 to 255, for storage in the unsigned char.
My suggestion to you is just try to compile your code without any casts. If it compiles, then all is fine and dandy. If it doesn't, *THEN* you can go playing with casts.
The general rule is, avoid casts as much as possible. Use of casts in
C++ usually indicates bad programming design and implementation (in C
things are different).
And if you have to use a cast, use the safest one (and *never* use the C
style one that you use above).
Regards,
Ioannis Vranos
"JKop" <NU**@NULL.NULL> wrote in message
news:h7*****************@news.indigo.ie... Busin posted:
In the code below, should i be casted to "unsigned int" type first
before passing it into f()? Thanks!
void f(unsigned int);
int main() { unsigned short i; i = 23; /* Should i be casted to "unsigned int" type first before passing it into f()? */ f(static_cast<unsigned int>(i)) } That's what I myself call an implicit conversion, ie. you don't have to explicitly specify that it is to be converted. Consider:
unsigned char snake = 12;
unsigned long int lizard = 4000000000UL;
snake = lizard;
You may be thinking here, Oh No! There's not enough room in that unsigned char for that number! You may be tempted to write:
snake = (unsigned char)lizard;
But it's unneccesary. It's done "automatically", "implicitly". The
unsigned long int number is truncated, most likely from 4000000000 to 255, for storage in the unsigned char.
Well, in general, that's going to be a bad thing, isn't it? You just lost
some data. The compiler should warn you about doing that, until you add the
cast, which makes the warning go away and makes you think you're safe, which
you might not be. (You might have needed that data!)
In the OP's question, there was no loss of data. You can always put an
unsigned short into an unsigned int, with no need for casting. There will
be no warning, because the data will always fit.
In the OP's case, the implicit conversion is a promotion from unsigned short
to unsigned int, which is different from trying to stick a long into a char.
My suggestion to you is just try to compile your code without any casts.
If it compiles, then all is fine and dandy. If it doesn't, *THEN* you can go playing with casts.
I agree you should avoid casts whenever possible (and especially avoid
C-style casts in C++). But just because it compiles, don't assume it works.
Heed compiler warnings, especially in cases where you might lose data!
-Howard
JKop <NU**@NULL.NULL> wrote: unsigned char snake = 12; unsigned long int lizard = 4000000000UL;
snake = lizard;
You may be thinking here, Oh No! There's not enough room in that unsigned char for that number! You may be tempted to write:
snake = (unsigned char)lizard;
But it's unneccesary. It's done "automatically", "implicitly". The unsigned long int number is truncated, most likely from 4000000000 to 255, for storage in the unsigned char.
Actually, it will never be 255. As you have been told in another thread,
the rules for unsigned overflow are well-defined. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: lokb |
last post by:
Hi,
I have a structure which and defined a smart pointer to the structure.
/* Structure of Begin Document Index Record */
typedef struct BDI_Struct{
unsigned char rname;
unsigned short int...
|
by: Alf P. Steinbach |
last post by:
The C++ FAQ item 29.5 (this seems to be strongly related to C), at
<url: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.5>
mentions that
<quote>
C++ guarantees a char is exactly one...
|
by: TTroy |
last post by:
Hello, I'm relatively new to C and have gone through more than 4 books
on it. None mentioned anything about integral promotion, arithmetic
conversion, value preserving and unsigned preserving. ...
|
by: Pravin |
last post by:
I have a function as follows in managed c++.
public foo( object *objectValue)
{
// pseudo code below, as I don't have exact code at this very moment.
if( type of objectValue is int)
{...
|
by: Pat Ireland |
last post by:
The following code shows what I am trying to do. The normal invocation of
the CalcByRef produces the correct results, however, using the InvokeMember
fails to correctly pass the single argument by...
|
by: dbuchanan |
last post by:
Hello,
Here is the error message;
----------------------------
Exception Message:
ForeignKeyConstraint Lkp_tbl040Cmpt_lkp302SensorType requires the child
key values (5) to exist in the...
|
by: John Bailo |
last post by:
I want to pass a SqlCommand object as a input parameter to a method.
I want to pass the SqlCommand "by value" so that any updates to the
original object are *not* reflected in the object within...
|
by: mimsc |
last post by:
This is my database code:
public List getListingData() {
//clear the list
managerList.clear();
//fill the list with new data
try {
open();
...
|
by: raylopez99 |
last post by:
Keywords: scope resolution, passing classes between parent and child
forms, parameter constructor method, normal constructor, default
constructor, forward reference, sharing classes between forms....
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |