473,473 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

reinterpret_cast< complex<double>* > of double*

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[i]<<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
Feb 27 '07 #1
3 6125
On 27 Feb, 09:47, "J.M." <j...@gmx.dewrote:
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

Feb 27 '07 #2
J.M. wrote:
>
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/
Mar 6 '07 #3
On Feb 27, 7:19 pm, "kwikius" <a...@servocomm.freeserve.co.ukwrote:
On 27 Feb, 09:47, "J.M." <j...@gmx.dewrote:
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?

Mar 6 '07 #4

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

Similar topics

11
by: Scott Brady Drummonds | last post by:
Hi, everyone, I've checked a couple of on-line resources and am unable to determine how reinterpret_cast<> is different from static_cast<>. They both seem to perform a compile-time casting of...
4
by: adamrobillard | last post by:
Hi, I have always used fopen and FILE* to load and save structures to file. I am trying to convert all the older code to use proper C++ calls... the following code works properly but I would...
27
by: Noah Roberts | last post by:
What steps do people take to make sure that when dealing with C API callback functions that you do the appropriate reinterpret_cast<>? For instance, today I ran into a situation in which the wrong...
9
by: richard_lavoie | last post by:
Hi, I have something like this: vector<floatvec1; and I want to cast it, so I use vector vec2<double= static_cast< vector<double(vec1); I always become a error: syntax error before `>'...
3
by: huili80 | last post by:
Like in the following. Though it gives the expected result with gcc4.0, is it really safe to do that? What if it's not double but some non-POD type? -----------------------------------------...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.