The correct cast

P

persres

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
 
V

Victor Bazarov

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
 
V

Victor Bazarov

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
 
U

Ulrich Eckhardt

Victor said:
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
 
U

Ulrich Eckhardt

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
 
P

Paul Brettschneider

Ulrich said:
I'd say it returns a size_t instead.


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
 
U

Ulrich Eckhardt

Paul said:
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
 
P

Paul Brettschneider

Ulrich said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top