Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: conversion to char*

Reply
Thread Tools

Re: conversion to char*

 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-31-2003
"Marc Schellens" <(E-Mail Removed)> wrote...
> I want to convert, an eg. long int* to a char*
> But using a static_cast<char*>(...) the compiler complains.


How?

> Why?


Without knowing how it's impossible to tell.

> So I am using a reinterpret_cast<char*>(...)
> I know that the long int* points to a valid memeory location.
> Is the reinterpret cast then dangerous, ie.
> can I assume that pointers to differnt types have the same length,
> and all other properties?


What are you going to do with the char*?

Victor


 
Reply With Quote
 
 
 
 
Marc Schellens
Guest
Posts: n/a
 
      07-31-2003
>>I want to convert, an eg. long int* to a char*
>>But using a static_cast<char*>(...) the compiler complains.

>
>
> How?


Edited the program in between...
something like:
cannot static_cast from MyType* to char*


>>Why?

>
>
> Without knowing how it's impossible to tell.
>
>
>>So I am using a reinterpret_cast<char*>(...)
>>I know that the long int* points to a valid memeory location.
>>Is the reinterpret cast then dangerous, ie.
>>can I assume that pointers to differnt types have the same length,
>>and all other properties?

>
>
> What are you going to do with the char*?


char *src,*dest;
size_t byt_length;
copy( src, src+byt_length, dest);

I cannot use the original types,
because the task is to copy an array of some type to another
type, bytewise.


> Victor

thanks,
marc


 
Reply With Quote
 
 
 
 
Marc Schellens
Guest
Posts: n/a
 
      07-31-2003
>>I want to convert, an eg. long int* to a char*
>>But using a static_cast<char*>(...) the compiler complains.

>
>
> How?


Edited the program in between...
something like:
cannot static_cast from MyType* to char*


>>Why?

>
>
> Without knowing how it's impossible to tell.
>
>
>>So I am using a reinterpret_cast<char*>(...)
>>I know that the long int* points to a valid memeory location.
>>Is the reinterpret cast then dangerous, ie.
>>can I assume that pointers to differnt types have the same length,
>>and all other properties?

>
>
> What are you going to do with the char*?


char *src,*dest;
size_t byt_length;
copy( src, src+byt_length, dest);

I cannot use the original types,
because the task is to copy an array of some type to another
type, bytewise.


> Victor

thanks,
marc


 
Reply With Quote
 
Marc Schellens
Guest
Posts: n/a
 
      07-31-2003
Victor Bazarov wrote:
> "Marc Schellens" <(E-Mail Removed)> wrote...
>
>>I want to convert, an eg. long int* to a char*
>>But using a static_cast<char*>(...) the compiler complains.

>
>
> How?


ok, got it back:

basic_fun.cpp:481: invalid static_cast from type `DByte*' to type `char*'

But I think ES Kim already gave the answer to this.
Thanks ES Kim.

>
>>Why?

>
>
> Without knowing how it's impossible to tell.
>
>
>>So I am using a reinterpret_cast<char*>(...)
>>I know that the long int* points to a valid memeory location.
>>Is the reinterpret cast then dangerous, ie.
>>can I assume that pointers to differnt types have the same length,
>>and all other properties?

>
>
> What are you going to do with the char*?


Please see my post in the other thread (somehow it went there...)

> Victor


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-31-2003
"Marc Schellens" <(E-Mail Removed)> wrote...
> >>So I am using a reinterpret_cast<char*>(...)
> >>I know that the long int* points to a valid memeory location.
> >>Is the reinterpret cast then dangerous, ie.
> >>can I assume that pointers to differnt types have the same length,
> >>and all other properties?

> >
> >
> > What are you going to do with the char*?

>
> char *src,*dest;
> size_t byt_length;
> copy( src, src+byt_length, dest);
>
> I cannot use the original types,
> because the task is to copy an array of some type to another
> type, bytewise.


Strange task. Not portable. Use memcpy(), it will convert the
pointers to 'void*'.

Victor


 
Reply With Quote
 
Marc Schellens
Guest
Posts: n/a
 
      07-31-2003


Victor Bazarov wrote:
> "Marc Schellens" <(E-Mail Removed)> wrote...
>
>>>>So I am using a reinterpret_cast<char*>(...)
>>>>I know that the long int* points to a valid memeory location.
>>>>Is the reinterpret cast then dangerous, ie.
>>>>can I assume that pointers to differnt types have the same length,
>>>>and all other properties?
>>>
>>>
>>>What are you going to do with the char*?

>>
>>char *src,*dest;
>>size_t byt_length;
>>copy( src, src+byt_length, dest);
>>
>>I cannot use the original types,
>>because the task is to copy an array of some type to another
>>type, bytewise.

>
>
> Strange task. Not portable. Use memcpy(), it will convert the
> pointers to 'void*'.


Its an interpreter and the language provides this kind of copy...
Yes, memcpy(...) is more appropriate here I think.
But why is this not portable?
I can static_cast to void*

And: If c++ does not guarntee that pointers to differnt tyes
have the same properties, why one can static_cast to void* then?

And stop!
Actually I copy valarrays.
Eg. valarray<long int> to valarray< char>
so
char* src (and dest) is set to &v[0] (v being a valarray -
of course different valarrays for src and dest, size is checked before).

Might there be something evil with the alignment?

thanks,
marc



 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-31-2003
"Marc Schellens" <(E-Mail Removed)> wrote...
>
>
> Victor Bazarov wrote:
> > "Marc Schellens" <(E-Mail Removed)> wrote...
> >
> >>>>So I am using a reinterpret_cast<char*>(...)
> >>>>I know that the long int* points to a valid memeory location.
> >>>>Is the reinterpret cast then dangerous, ie.
> >>>>can I assume that pointers to differnt types have the same length,
> >>>>and all other properties?
> >>>
> >>>
> >>>What are you going to do with the char*?
> >>
> >>char *src,*dest;
> >>size_t byt_length;
> >>copy( src, src+byt_length, dest);
> >>
> >>I cannot use the original types,
> >>because the task is to copy an array of some type to another
> >>type, bytewise.

> >
> >
> > Strange task. Not portable. Use memcpy(), it will convert the
> > pointers to 'void*'.

>
> Its an interpreter and the language provides this kind of copy...
> Yes, memcpy(...) is more appropriate here I think.
> But why is this not portable?


I think you're onto something with alignment. Relative sizes
of objects and char differ with platforms and implementations.
'memcpy' is implementation-specific, so they can play tricks
inside, of which you don't have to know.

> I can static_cast to void*


Yes, you can. It's explicitly allowed.

> And: If c++ does not guarntee that pointers to differnt tyes
> have the same properties, why one can static_cast to void* then?


A pointer to void* is a special pointer. You can convert any
other pointer to an object to it and back and it should yield
the same value.

> And stop!
> Actually I copy valarrays.
> Eg. valarray<long int> to valarray< char>


....mmm.. I don't think it's a good idea. 'memcpy' is not going
to help you. The Standard only allows copying PODs with memcpy.

> so
> char* src (and dest) is set to &v[0] (v being a valarray -
> of course different valarrays for src and dest, size is checked before).
>
> Might there be something evil with the alignment?


Sure there might. Why do you think you need to copy a valarray
of long int into a valarray of char?

Victor


 
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
PSD to XHTML Conversion Services and PSD to HTML CSS ConversionServices, PSD to Joomla, Drupal, Wordpress Conversion xhtml champs Python 0 06-21-2011 11:59 AM
PSD to XHTML Conversion Services and PSD to HTML CSS ConversionServices, PSD to Joomla, Drupal, Wordpress Conversion PSD to XHTML Conversion Services and PSD to HTML CSS Conversion Services, PSD to Joomla, Drupal, Wor VHDL 0 04-25-2011 06:43 AM
conversion operator and conversion ctor subramanian100in@yahoo.com, India C++ 2 09-15-2009 12:46 PM
Date conversion problem with OE importing saroxonline76@vodafone.it Firefox 0 07-12-2005 07:38 PM
Framework 1.0 to 1.1 conversion Luc Bisson ASP .Net 2 11-19-2003 02:40 AM



Advertisments