reinterpret_cast< complex<double>* > of double*

J

J.M.

I have data in a double array of length 2N, which actually represents
complex numbers with real and imaginary parts interlaced. In other words,
elements in this array with even indices represents the real part of a
complex number, elements with odd indices represent the imaginary part. I
actually need an array of length N of type complex<double>. Obviously, I
could copy the data and destroy the first array, but this is an expensive
option. The following seems to work as well:



double* d;
complex<double>* C;


d = new double[6];

d[0] = 1.0;
d[1] = 2.0;
d[2] = -3.0;
d[3] = -4.0;
d[4] = 5.0;
d[5] = -6.0;


C = reinterpret_cast< complex<double>* > (d);

for(int i=0;i<3;i++){
cout<<" i= "<<i<<": "<<C<<std::endl;
}

delete [] C;


Is this conversion safe, reliable, somehow standard? If it is likely to
produce unpredictable results, I would prefer copying the data.... I need
to be on the safe side, but speed is an issue... And my results should not
be compiler dependent ;-) Thanks in advance for any comments.

Jan
 
K

kwikius

Is this conversion safe, reliable, somehow standard? If it is likely to
produce unpredictable results, I would prefer copying the data.... I need
to be on the safe side, but speed is an issue... And my results should not
be compiler dependent ;-) Thanks in advance for any comments.

It usually works but is not mandated by the standard. There was a
discussion on comp.std.c++ some years back on the subject about making
this legal.

In fact many (most?) implementations actually use an array[2] for
double internally in complex, in which case the cast is very likely to
succeed but technically not guaranteed, because there still could be
padding applied, though as doubles are usually critical for alignment
this is unlikely in practise. I would do some compile time checks to
check that sizeof(complex<double>[2]) == sizeof(double[4]) , and if it
passes which IMO it probably will, consider it safe and go on my merry
way.

HTH

regards
Andy Little
 
G

Grizlyk

J.M. said:
C = reinterpret_cast< complex<double>* > (d);
Is this conversion safe, reliable, somehow standard?

I think "reinterpret_cast" can be safe used to setup correct data type from
incorrect only, instead of reverse work.

For example, you can have pointer to int, that due to compile time
limitations really pointed to runtime memory allocated as double. You can
use "reinterpret_cast" here ("void*" and "static_cast" is better replacement
of "reinterpret_cast" in the case). But if you have pointer to double, that
really pointed to double, you can not conver it into pointer to int without
danger.

In general case the class "complex" has hidden implementation and memory
layout, so it will be point of error here if user will replace class
"complex" implementation, expecting that program will use complex interface
only.

In the case of work with concrete class "complex" implementation you
probably can, but it is better to encapsulate the conversion into class,
probably friend to the concrete class "complex" implementation.


--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
 
S

Sarath

Is this conversion safe, reliable, somehow standard? If it is likely to
produce unpredictable results, I would prefer copying the data.... I need
to be on the safe side, but speed is an issue... And my results should not
be compiler dependent ;-) Thanks in advance for any comments.

It usually works but is not mandated by the standard. There was a
discussion on comp.std.c++ some years back on the subject about making
this legal.

In fact many (most?) implementations actually use an array[2] for
double internally in complex, in which case the cast is very likely to
succeed but technically not guaranteed, because there still could be
padding applied, though as doubles are usually critical for alignment
this is unlikely in practise. I would do some compile time checks to
check that sizeof(complex<double>[2]) == sizeof(double[4]) , and if it
passes which IMO it probably will, consider it safe and go on my merry
way.

HTH

regards
Andy Little

Is it possible to iterator with this type of implementation?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top