Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   how to deal with the translation from "const char * " to "const unsigned char *"? (http://www.velocityreviews.com/forums/t460125-how-to-deal-with-the-translation-from-const-char-to-const-unsigned-char.html)

=?gb2312?B?wNbA1rTzzOzKpg==?= 01-29-2007 07:21 AM

how to deal with the translation from "const char * " to "const unsigned char *"?
 
i wrote:
-----------------------------------------------------------------------
----------------------------------------
unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");
sizeof(reinterpret_cast<const char *>(p));
-----------------------------------------------------------------------
----------------------------------------

the compiler tells me that "reinterpret_cast from type "const char * "
to type "unsigned char *" casts away constness "


Kaz Kylheku 01-29-2007 07:37 AM

Re: how to deal with the translation from "const char * " to "const unsigned char *"?
 


On Jan 28, 11:21 pm, "乐乐大天师" <chendong...@gmail.com> wrote:
> i wrote:-----------------------------------------------------------------------
> ----------------------------------------
> unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");
> sizeof(reinterpret_cast<const char *>(p));
> -----------------------------------------------------------------------
> ----------------------------------------
>
> the compiler tells me that "reinterpret_cast from type "const char * "
> to type "unsigned char *" casts away constness "


At this point you stop wrestling with the C++ bullshit and just use a
C style cast.

The good old C style cast will implicitly produce the correct
combination of reinterpret_cast and static_cast, without the verbiage
cluttering your code.

Since no typedef names are involved whose meanings could change, the C
style cast is no less safe than the new style cast combination, and is
much more readable.

(If you use the C cast with abstracted types, like casting some A * to
some B *, the danger is that someone may redefine what A and B is, and
the cast will silently continue to work, even though the change may be
such that the conversion has erroneous consequences).



Kai-Uwe Bux 01-29-2007 08:17 AM

Re: how to deal with the translation from "const char * " to "const unsigned char *"?
 
????? wrote:

> i wrote:
> -----------------------------------------------------------------------
> ----------------------------------------
> unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");
> sizeof(reinterpret_cast<const char *>(p));
> -----------------------------------------------------------------------
> ----------------------------------------
>
> the compiler tells me that "reinterpret_cast from type "const char * "
> to type "unsigned char *" casts away constness "


In the code you posted, you do not cast to "const unsigned char *". Try:

unsigned char const * p = static_cast<unsigned char const *>("abcdg");


Best

Kai-Uwe Bux

Gianni Mariani 01-29-2007 08:21 AM

Re: how to deal with the translation from "const char * " to "constunsigned char *"?
 
乐乐大天师 wrote:
> i wrote:
> -----------------------------------------------------------------------
> ----------------------------------------
> unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");
> sizeof(reinterpret_cast<const char *>(p));
> -----------------------------------------------------------------------
> ----------------------------------------
>
> the compiler tells me that "reinterpret_cast from type "const char * "
> to type "unsigned char *" casts away constness "
>



The compiler is right. "unsigned char *" is non-const and string
literals (like "abcdg") are const.


If you're casting to a "unsigned char *" you need to ask yourself why.
Will it be modified after the cast ? (that would be undefined and toast
your process on some platforms)

It seems like you have a design problem.

Noah Roberts 01-29-2007 08:57 AM

Re: how to deal with the translation from "const char * " to "constunsigned char *"?
 
Gianni Mariani wrote:
> 乐乐大天师 wrote:
>> i wrote:
>> -----------------------------------------------------------------------
>> ----------------------------------------
>> unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");
>> sizeof(reinterpret_cast<const char *>(p));
>> -----------------------------------------------------------------------
>> ----------------------------------------
>>
>> the compiler tells me that "reinterpret_cast from type "const char * "
>> to type "unsigned char *" casts away constness "
>>

>
>
> The compiler is right. "unsigned char *" is non-const and string
> literals (like "abcdg") are const.


It always intrigues me that such distinction is made in cases like this
yet this is perfectly ok from a legal point of view:

char * x = "I'm const";

Gianni Mariani 01-29-2007 09:01 AM

Re: how to deal with the translation from "const char * " to "constunsigned char *"?
 
Noah Roberts wrote:
> Gianni Mariani wrote:

....
>
> It always intrigues me that such distinction is made in cases like this
> yet this is perfectly ok from a legal point of view:
>
> char * x = "I'm const";


Yes, sigh. I think the standard's commitee made a blunder on that one.

This is due to a rather short sighted view that there was a large legacy
code base that would break if that was not allowed. At a guess, I think
this would not be in the standard as an exception if hindsight was
available.

Grizlyk 01-29-2007 09:12 AM

Re: how to deal with the translation from "const char * " to "const unsigned char *"?
 
Gianni Mariani wrote:
>>
>> unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");
>>
>> the compiler tells me that "reinterpret_cast from type "const char * " to
>> type "unsigned char *" casts away constness "

>
> The compiler is right. "unsigned char *" is non-const and string literals
> (like "abcdg") are const.
>
>
> If you're casting to a "unsigned char *" you need to ask yourself why.


I think, the following expression
unsigned char * p = static_cast<unsigned char *>( const_cast<char*>
("abcdg") );
is not work because sizeof("type") can be not equal to sizeof("unsigned
type") in theory,

Assuming on your target sizeof("type")==sizeof("unsigned type")

If "p" is _not_ const because there is old library parameter here and you
are shure, that "p" is always using as "const unsigned char *" you can apply
C-style cast
(unsigned char *) ("abcdg")"
or reinterpret_cast<>
unsigned char * p = reinterpret_cast<unsigned char *>( const_cast<char*>
("abcdg") );

else you can create a temporary storage

const char data[]="abcdg";
char buf[sizeof(data)]; strcpy(buf,data);
unsigned char * p = reinterpret_cast<unsigned char *>( buf );

--
Maksim A Polyanin



Grizlyk 01-29-2007 09:45 AM

Re: how to deal with the translation from "const char * " to "const unsigned char *"?
 
Grizlyk wrote:

> or reinterpret_cast<>
> unsigned char * p = reinterpret_cast<unsigned char *>(
> const_cast<char*> ("abcdg") );


And i forget cast via "void*"

unsigned char * p = static_cast<void*> ("abcdg");

--
Maksim A Polyanin



Grizlyk 01-29-2007 10:18 AM

Re: how to deal with the translation from "const char * " to "const unsigned char *"?
 
Grizlyk wrote:
>> or reinterpret_cast<>
>> unsigned char * p = reinterpret_cast<unsigned char *>(
>> const_cast<char*> ("abcdg") );

>
> And i forget cast via "void*"
>
> unsigned char * p = static_cast<void*> ("abcdg");


No, can not, i was wrong, better to do like this

void *const tmp = const_cast<char*>("abcdg");
unsigned char *p = static_cast<unsigned char*>(tmp);

--
Maksim A Polyanin



Rolf Magnus 01-29-2007 11:32 AM

Re: how to deal with the translation from "const char * " to "const unsigned char *"?
 
Kaz Kylheku wrote:

>
>
> On Jan 28, 11:21 pm, "乐乐大天师" <chendong...@gmail.com> wrote:
>> i
>>

wrote:-----------------------------------------------------------------------
>> ---------------------------------------- unsigned char * p =
>> reinterpret_cast<unsigned char *>("abcdg"); sizeof(reinterpret_cast<const
>> char *>(p));
>> -----------------------------------------------------------------------
>> ----------------------------------------
>>
>> the compiler tells me that "reinterpret_cast from type "const char * "
>> to type "unsigned char *" casts away constness "

>
> At this point you stop wrestling with the C++ bullshit and just use a
> C style cast.


No. At this point you think thrice about letting a pointer to non-const
point to a string literal, and twice about letting a pointer to unsigned
char point to a char.
For the second, there is rarely a reason. For the first, the only reason I
can think of is to deal with legacy code that isn't const correct.

> The good old C style cast will implicitly produce the correct
> combination of reinterpret_cast and static_cast, without the verbiage
> cluttering your code.


No. Rather it produces any combination that will somehow make the target
type from the supplied object. It doesn't magically know if that's the
correct one. So better explicitly tell the compiler which conversion you
actually want.

> Since no typedef names are involved whose meanings could change, the C
> style cast is no less safe than the new style cast combination, and is
> much more readable.


Well, with the C style cast, the OP wouldn't have noticed that he let a
non-const pointer point to something that is const and must not be changed.
The compiler would have silently accepted the erroneous code.

> (If you use the C cast with abstracted types, like casting some A * to
> some B *, the danger is that someone may redefine what A and B is, and
> the cast will silently continue to work, even though the change may be
> such that the conversion has erroneous consequences).


It always does silently whatever it takes, so it's always dangerous.



All times are GMT. The time now is 07:15 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.