C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Pass value of short type

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Busin
Guest





PostPosted: Sun Jun 27, 2004 4:00 am    Post subject: Pass value of short type Reply with quote



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))
}




Back to top
Alf P. Steinbach
Guest





PostPosted: Sun Jun 27, 2004 4:13 am    Post subject: Re: Pass value of short type Reply with quote



* Busin:
Quote:
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?

Back to top
JKop
Guest





PostPosted: Sun Jun 27, 2004 10:10 am    Post subject: Re: Pass value of short type Reply with quote



Alf P. Steinbach posted:

Quote:
* 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

Back to top
JKop
Guest





PostPosted: Sun Jun 27, 2004 10:15 am    Post subject: Re: Pass value of short type Reply with quote

Busin posted:

Quote:
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

Back to top
Ioannis Vranos
Guest





PostPosted: Sun Jun 27, 2004 6:49 pm    Post subject: Re: Pass value of short type Reply with quote

Busin wrote:
Quote:
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

Back to top
Ioannis Vranos
Guest





PostPosted: Sun Jun 27, 2004 6:58 pm    Post subject: Re: Pass value of short type Reply with quote

JKop wrote:

Quote:
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.




Quote:
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

Back to top
Howard
Guest





PostPosted: Mon Jun 28, 2004 3:44 pm    Post subject: Re: Pass value of short type Reply with quote


"JKop" <NULL (AT) NULL (DOT) NULL> wrote

Quote:
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.

Quote:
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



Back to top
Old Wolf
Guest





PostPosted: Tue Jun 29, 2004 2:30 am    Post subject: Re: Pass value of short type Reply with quote

JKop <NULL (AT) NULL (DOT) NULL> wrote:
Quote:

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.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.