Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > The correct cast

Reply
Thread Tools

The correct cast

 
 
persres@googlemail.com
Guest
Posts: n/a
 
      01-13-2011
Hi,
I want to know if reinterpret_cast is the correct one here. I want
to know what are the dangers or issues of doing this.

unsigned Len=0;
unsigned char buf[] = { 65, 66, 67, 0 };
Len = strlen(reinterpret_cast<char *>(buf));

Why should the compiler give a warning at all. Cant it just do the
conversion?. Seems harmless. Your thoughts please.
Thanks
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-13-2011
On 1/13/2011 10:52 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi,
> I want to know if reinterpret_cast is the correct one here. I want
> to know what are the dangers or issues of doing this.
>
> unsigned Len=0;
> unsigned char buf[] = { 65, 66, 67, 0 };
> Len = strlen(reinterpret_cast<char *>(buf));
>
> Why should the compiler give a warning at all. Cant it just do the
> conversion?. Seems harmless. Your thoughts please.


I think a 'static_cast' is better in this case. Also, doesn't 'strlen'
return an int? Assigning it to an unsigned int might also cause a warning.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-13-2011
On 1/13/2011 11:08 AM, Leigh Johnston wrote:
> On 13/01/2011 15:59, Victor Bazarov wrote:
>> On 1/13/2011 10:52 AM, (E-Mail Removed) wrote:
>>> Hi,
>>> I want to know if reinterpret_cast is the correct one here. I want
>>> to know what are the dangers or issues of doing this.
>>>
>>> unsigned Len=0;
>>> unsigned char buf[] = { 65, 66, 67, 0 };
>>> Len = strlen(reinterpret_cast<char *>(buf));
>>>
>>> Why should the compiler give a warning at all. Cant it just do the
>>> conversion?. Seems harmless. Your thoughts please.

>>
>> I think a 'static_cast' is better in this case. Also, doesn't 'strlen'
>> return an int? Assigning it to an unsigned int might also cause a
>> warning.
>>

>
> static_cast would be incorrect (types are unrelated) so the warning is
> probably down to "Len".


Well, shows how much I've used casts in the recent years... You're
right, reinterpret_cast is the one, shouldn't be any warning. Of
course, compilers are free to issue any warning at any time, they are
not mandated, nor is the absence of them.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      01-13-2011
Victor Bazarov wrote:
> Also, doesn't 'strlen' return an int?


I'd say it returns a size_t instead.

> Assigning it to an unsigned int might also cause a warning.


Yes, but because an unsigned int might be smaller, not because it is
signed.

Uli

 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      01-13-2011
(E-Mail Removed) wrote:
> I want to know if reinterpret_cast is the correct one here. I want
> to know what are the dangers or issues of doing this.


Issues are only with correctness, there are no actual dangers.

> unsigned char buf[] = { 65, 66, 67, 0 };
> reinterpret_cast<char *>(buf)


What you can do is use a static_cast to "void*" and then to "char*" (or
their const equivalents), which would be fully defined and correct.
Another alternative would be to use a C-style cast instead. I'd not use
the reinterpret_cast, because it suggests something implementation-
defined.

Or, what you can do in this case is to simply use "sizeof buf - 1" or use
std::vector instead, but that won't work for general unsigned char
pointers.

Uli

 
Reply With Quote
 
Paul Brettschneider
Guest
Posts: n/a
 
      01-13-2011
Ulrich Eckhardt wrote:

> Victor Bazarov wrote:
>> Also, doesn't 'strlen' return an int?

>
> I'd say it returns a size_t instead.
>
>> Assigning it to an unsigned int might also cause a warning.

>
> Yes, but because an unsigned int might be smaller, not because it is
> signed.


Unless you tell the compiler to:

$ cat x.C
int main()
{
int i = 5;
unsigned int j = i;
return j;
}
$ g++ -Wsign-conversion -O3 x.C
x.C: In function ‘int main()’:
x.C:4: warning: conversion to ‘unsigned int’ from ‘int’ may change the sign
of the result
x.C:5: warning: conversion to ‘int’ from ‘unsigned int’ may change the sign
of the result

 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      01-13-2011
Paul Brettschneider wrote:
> Ulrich Eckhardt wrote:
>> Victor Bazarov wrote:
>>> Also, doesn't 'strlen' return an int?

>>
>> I'd say it returns a size_t instead.
>>
>>> Assigning it to an unsigned int might also cause a warning.

>>
>> Yes, but because an unsigned int might be smaller, not because it is
>> signed.

>
> Unless you tell the compiler to:
>
> $ cat x.C
> int main()
> {
> int i = 5;
> unsigned int j = i;
> return j;
> }
> $ g++ -Wsign-conversion -O3 x.C
> x.C: In function ‘int main()’:
> x.C:4: warning: conversion to ‘unsigned int’ from ‘int’ may change the
> sign of the result
> x.C:5: warning: conversion to ‘int’ from ‘unsigned int’ may change the
> sign of the result


Let me fix what I mean: Assigning the result of strlen() to an "unsigned"
can not yield a warning due to an assignment with signed/unsigned
conversion. It can only yield a warning because "unsigned" might have a
different size than "size_t". Sorry for the confusion.

Uli

 
Reply With Quote
 
Paul Brettschneider
Guest
Posts: n/a
 
      01-13-2011
Ulrich Eckhardt wrote:

> Paul Brettschneider wrote:
>> Ulrich Eckhardt wrote:
>>> Victor Bazarov wrote:
>>>> Also, doesn't 'strlen' return an int?
>>>
>>> I'd say it returns a size_t instead.
>>>
>>>> Assigning it to an unsigned int might also cause a warning.
>>>
>>> Yes, but because an unsigned int might be smaller, not because it is
>>> signed.

>>
>> Unless you tell the compiler to:
>>
>> $ cat x.C
>> int main()
>> {
>> int i = 5;
>> unsigned int j = i;
>> return j;
>> }
>> $ g++ -Wsign-conversion -O3 x.C
>> x.C: In function ‘int main()’:
>> x.C:4: warning: conversion to ‘unsigned int’ from ‘int’ may change the
>> sign of the result
>> x.C:5: warning: conversion to ‘int’ from ‘unsigned int’ may change the
>> sign of the result

>
> Let me fix what I mean: Assigning the result of strlen() to an "unsigned"
> can not yield a warning due to an assignment with signed/unsigned
> conversion. It can only yield a warning because "unsigned" might have a
> different size than "size_t". Sorry for the confusion.


You are right - because size_t _is_ unsigned.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast? Pavel C++ 7 09-18-2010 11:35 PM
Correct way to cast Joe C Programming 9 03-15-2007 07:48 AM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM
malloc - to cast or not to cast, that is the question... EvilRix C Programming 8 02-14-2004 12:08 PM
to cast or not to cast malloc ? MSG C Programming 38 02-10-2004 03:13 PM



Advertisments