Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: conversion to char*

 
 
Michiel Salters
Guest
Posts: n/a
 
      08-01-2003
Marc Schellens <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> >>I want to convert, an eg. long int* to a char*
> >>But using a static_cast<char*>(...) the compiler complains.
> >>Why?

> >
> > static_cast<typename>(expression) is valid only if "typename"
> > can be converted implicitly to the same type of "expression",
> > or vice versa. There's no implicit conversion between two
> > different pointer types.
> >
> >
> >>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?

> >
> >
> > No, you can't.


Different pointer types may have different representations.
unsigned char* and void* are guaranteed to have a an universal
representation though, so you can cast any poiter to both types
without information loss.

> So what to do?
> But de facto (speaking of windows, x86 linux, apple, sun...)
> I can at least?
> (I have never seen pointers of different length for eg in C on
> these platforms (within one program)...)
> Additionally I know that the types involved are the arrays of
> different integer type, floats and doubles.
> Are the poiters for them at least same length?


Probably, but what does that give you? The pointers certainly
can have different alignment requirements. On the platforms
you mention, 50% of the float* couldn't point to a double.
You can use memcpy though, or casts to unsigned char*. Both
float and double are so-called POD types, which can be read
and written as if they were char arrays. This can be used
only as a transfer format; the only way to use these bytes
is by ensuring these bytes end up in suitably aligned memory
( e.g. allocated by malloc(), or another double resp. float
object ). When you

> If yes:
> If I add complex<float> and complex<double> still?


No, these types aren't PODs. You can "read" them by casting
their addresses to unsigned char*, but you can't do anything
useful with the result, you can't even use it to create
a hash key. You certainly can't set a complex<> to a certain
value by writing directly to the unsigned char[]
representation.

HTH,
--
Michiel Salters
 
Reply With Quote
 
 
 
 
Kevin Goodsell
Guest
Posts: n/a
 
      08-01-2003
Michiel Salters wrote:

>
>
> Different pointer types may have different representations.
> unsigned char* and void* are guaranteed to have a an universal
> representation though, so you can cast any poiter to both types
> without information loss.
>


You can cast any *object pointer* to either type without loss. Function
pointers are a different matter.

-Kevin

 
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