472,972 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,972 software developers and data experts.

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 "

Jan 29 '07 #1
26 11403


On Jan 28, 11:21 pm, "乐乐大天师" <chendong...@gmail.comwrote:
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).
Jan 29 '07 #2
乐乐大天师 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.
Jan 29 '07 #3
????? 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
Jan 29 '07 #4
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";
Jan 29 '07 #5
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.
Jan 29 '07 #6
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
Jan 29 '07 #7
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
Jan 29 '07 #8
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
Jan 29 '07 #9
Kaz Kylheku wrote:
>

On Jan 28, 11:21 pm, "乐乐大天师" <chendong...@gmail.comwrote:
>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.

Jan 29 '07 #10
Gianni Mariani a crit :
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.
The standard comitee didn't want to constraint compilers with putting
the array in a rw area.
The compiler is free to put in ro and save some copy time. The array can
even be shared among other part of the program.

That is a reason why casting away the consteness is undefined in this
case and should not be done.

Michael
Jan 29 '07 #11
Michael DOUBEZ a crit :
Gianni Mariani a crit :
>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.

The standard comitee didn't want to constraint compilers with putting
the array in a rw area.
The compiler is free to put in ro and save some copy time. The array can
even be shared among other part of the program.

That is a reason why casting away the consteness is undefined in this
case and should not be done.
In fact, it is legacy code that allowed this writing because string use
to be defaulted as char[] and automaticaly casted to char*.
And the standard changed the type of string to const char[] and thus
strings can now only be automatically casted into const char*.

Michael

Jan 29 '07 #12
Michael DOUBEZ wrote:
Gianni Mariani a écrit :
>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.

The standard comitee didn't want to constraint compilers with putting
the array in a rw area.
The compiler is free to put in ro and save some copy time. The array can
even be shared among other part of the program.
What does this have to do with the choice to allow letting a pointer to
non-const point to string literals without a cast?
That is a reason why casting away the consteness is undefined in this
case and should not be done.
Casting away constness isn't undefined. Only writing to an object that was
initially declared const is.

Jan 29 '07 #13
Rolf Magnus a écrit :
Michael DOUBEZ wrote:
>The standard comitee didn't want to constraint compilers with putting
the array in a rw area.
The compiler is free to put in ro and save some copy time. The array can
even be shared among other part of the program.

What does this have to do with the choice to allow letting a pointer to
non-const point to string literals without a cast?
Why would you be allowed to cast it to not const if not to be able to
modify it ?
And if it is done automatically, there is the risk you modify the string
without remebering it is const.
>
>That is a reason why casting away the consteness is undefined in this
case and should not be done.

Casting away constness isn't undefined. Only writing to an object that was
initially declared const is.
Yes, writing is undefined; that is what I meant.

Michael
Jan 29 '07 #14
"Grizlyk" <gr******@yandex.ruwrote in news:ep**********@aioe.org:
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,
So? That code is casting pointers-to-object. The pointer size won't
change, regardless of the size of the pointed to object.
Jan 29 '07 #15
Andre Kostur wrote:
>I think, the following expression
unsigned char * p =
static_cast<unsigned char *>
(
const_cast<char*>("abcdg")
);
is do not work because sizeof("type") can be not equal
to sizeof("unsigned type") in theory

So? That code is casting pointers-to-object. The pointer size won't
change, regardless of the size of the pointed to object.
I am not shure (i have no standard) that pointers for all types must have
fixed sizeof() (as "int" for example) or unsigned type must have the same
sizeof() as signed type.

For example there are memory models, where "sizeof(char *_far)==4", but
"sizeof(char *)==2".

Let pointers have fixed sizeof() and sizeof("type")!=sizeof("unsigned
type"). Here pointer arithmetic can be changed, because value of next
pointer (++ptr) is depending from size of object pointer point to.

For example:

int *i=0;
++i; //value of i is 4

char *c=0;
++c; //value of c is 1

//assuming sizeof(int)!=sizeof(unsigned)
unsigned *u=0;
++u; //value of u is 8

--
Maksim A Polyanin
Jan 29 '07 #16
"Grizlyk" <gr******@yandex.ruwrote in news:ep**********@aioe.org:
Andre Kostur wrote:
>>I think, the following expression
unsigned char * p =
static_cast<unsigned char *>
(
const_cast<char*>("abcdg")
);
is do not work because sizeof("type") can be not equal
to sizeof("unsigned type") in theory

So? That code is casting pointers-to-object. The pointer size won't
change, regardless of the size of the pointed to object.

I am not shure (i have no standard) that pointers for all types must
have fixed sizeof() (as "int" for example) or unsigned type must have
the same sizeof() as signed type.

For example there are memory models, where "sizeof(char *_far)==4",
but "sizeof(char *)==2".
sizeof(char * _far) is a non-standard type, and thus is implementation-
dependant. All Standard pointers-to-object have the same size.
Let pointers have fixed sizeof() and sizeof("type")!=sizeof("unsigned
type"). Here pointer arithmetic can be changed, because value of next
pointer (++ptr) is depending from size of object pointer point to.
Pointer arithmetic has nothing to do with sizeof(T*). But is related to
sizeof(T).
Jan 29 '07 #17
Michael DOUBEZ wrote:
Rolf Magnus a écrit :
>Michael DOUBEZ wrote:
>>The standard comitee didn't want to constraint compilers with putting
the array in a rw area.
The compiler is free to put in ro and save some copy time. The array can
even be shared among other part of the program.

What does this have to do with the choice to allow letting a pointer to
non-const point to string literals without a cast?

Why would you be allowed to cast it to not const if not to be able to
modify it ?
And if it is done automatically, there is the risk you modify the string
without remebering it is const.
I think you've missed the point. The compiler will not complain about
code that does this:

char * x = "hello";
x[2] = 't';

You'll only get a warning when you try to run the program...if
then...maybe only when you create your release compile with its
optimizations...maybe a customer will find it.

The standard requires that this is ok. It is undefined, even in C where
it comes from, but there can be no diagnostic. Most people would like
to get the usual error about attempting to assign const to non-const
without a cast, but you don't get that in this one special case. Allows
for all sorts of unfortunate errors.
Jan 29 '07 #18
In article <ep**********@aioe.org>, "Grizlyk" <gr******@yandex.ru>
wrote:
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,
AFAIK, the above is incorrect. sizeof( "unsigned type" ) always equals
sizeof( "type" )
Jan 29 '07 #19
Andre Kostur wrote:
"Grizlyk" <gr******@yandex.ruwrote in news:ep**********@aioe.org:
>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,

So? That code is casting pointers-to-object. The pointer size won't
change, regardless of the size of the pointed to object.
Well, but using a pointer for accessing an object of a size different from
the pointed-to type might lead to trouble if you aren't prepared for it.

Btw: Different pointer types might also have different sizes.

Jan 29 '07 #20
Noah Roberts a écrit :
>
I think you've missed the point. The compiler will not complain about
code that does this:

char * x = "hello";
x[2] = 't';

You'll only get a warning when you try to run the program...if
then...maybe only when you create your release compile with its
optimizations...maybe a customer will find it.

The standard requires that this is ok. It is undefined, even in C where
it comes from, but there can be no diagnostic. Most people would like
to get the usual error about attempting to assign const to non-const
without a cast, but you don't get that in this one special case. Allows
for all sorts of unfortunate errors.
OK. My mistake.

Yes, it is misleading and only because K&R C (before 1988) does not have
the const keyword.

Michael
Jan 29 '07 #21
Noah Roberts wrote:
>
I think you've missed the point. The compiler will not complain about
code that does this:

char * x = "hello";
x[2] = 't';

You'll only get a warning when you try to run the program...if
then...maybe only when you create your release compile with its
optimizations...maybe a customer will find it.

The standard requires that this is ok. It is undefined, even in C where
it comes from, but there can be no diagnostic. Most people would like
to get the usual error about attempting to assign const to non-const
without a cast, but you don't get that in this one special case. Allows
for all sorts of unfortunate errors.
I don't think "there can be no diagnostic" is correct. A string literal
has a type of const char*, so the compiler may (and in my opinion
should) issue a diagnostic for "char* x = "hello";"

--
Ian Collins.
Jan 29 '07 #22
Ian Collins wrote:
Noah Roberts wrote:
The standard requires that this is ok. It is undefined, even in C
where it comes from, but there can be no diagnostic. Most people
would like to get the usual error about attempting to assign const
to non-const without a cast, but you don't get that in this one
special case. Allows for all sorts of unfortunate errors.

I don't think "there can be no diagnostic" is correct. A string
literal has a type of const char*, so the compiler may (and in my
opinion should) issue a diagnostic for "char* x = "hello";"
I was going to mention something about this. I believe it was just poor
wording on Noah's part, he meant "can" meaning "possible", I think.
It's possible there will be no diagnostic, because the standard doesn't
require one.


Brian
Jan 29 '07 #23
Default User wrote:
Ian Collins wrote:
>Noah Roberts wrote:
>>The standard requires that this is ok. It is undefined, even in C
where it comes from, but there can be no diagnostic. Most people
would like to get the usual error about attempting to assign const
to non-const without a cast, but you don't get that in this one
special case. Allows for all sorts of unfortunate errors.
I don't think "there can be no diagnostic" is correct. A string
literal has a type of const char*, so the compiler may (and in my
opinion should) issue a diagnostic for "char* x = "hello";"

I was going to mention something about this. I believe it was just poor
wording on Noah's part, he meant "can" meaning "possible", I think.
It's possible there will be no diagnostic, because the standard doesn't
require one.
Actually, I assumed it was explicitly stated not to since none do. I
couldn't find the place in the standard that says it is ok not to
actually.
Jan 30 '07 #24
Noah Roberts wrote:
Default User wrote:
>>Ian Collins wrote:

>>>Noah Roberts wrote:
>>>>The standard requires that this is ok. It is undefined, even in C
where it comes from, but there can be no diagnostic. Most people
would like to get the usual error about attempting to assign const
to non-const without a cast, but you don't get that in this one
special case. Allows for all sorts of unfortunate errors.

I don't think "there can be no diagnostic" is correct. A string
literal has a type of const char*, so the compiler may (and in my
opinion should) issue a diagnostic for "char* x = "hello";"

I was going to mention something about this. I believe it was just poor
wording on Noah's part, he meant "can" meaning "possible", I think.
It's possible there will be no diagnostic, because the standard doesn't
require one.


Actually, I assumed it was explicitly stated not to since none do. I
couldn't find the place in the standard that says it is ok not to
actually.
The closest is 2.13.4, which defines the type.

Sun CC issues a warning.

--
Ian Collins.
Jan 30 '07 #25
Compilers can issue warnings freely. Also, no compiler is 100%
standards-compliant.

Jan 30 '07 #26
Na********@gmail.com wrote:
Compilers can issue warnings freely. Also, no compiler is 100%
standards-compliant.
Please keep the context of the thread you are replying to.

--
Ian Collins.
Jan 30 '07 #27

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.