Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to deal with the translation from "const char * " to "const unsigned char *"?

Reply
Thread Tools

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

 
 
=?gb2312?B?wNbA1rTzzOzKpg==?=
Guest
Posts: n/a
 
      01-29-2007
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 "

 
Reply With Quote
 
 
 
 
Kaz Kylheku
Guest
Posts: n/a
 
      01-29-2007


On Jan 28, 11:21 pm, "乐乐大天师" <(E-Mail Removed)> 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).


 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      01-29-2007
????? 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
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      01-29-2007
乐乐大天师 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.
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      01-29-2007
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";
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      01-29-2007
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.
 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-29-2007
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


 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-29-2007
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


 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-29-2007
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


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      01-29-2007
Kaz Kylheku wrote:

>
>
> On Jan 28, 11:21 pm, "乐乐大天师" <(E-Mail Removed)> 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.

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
Printing the range s of unsigned char and unsigned int. Junmin H. C Programming 20 09-20-2007 06:03 AM
unsigned long to unsigned char ashtonn@gmail.com Python 1 06-01-2005 07:00 PM



Advertisments