reinterpret_cast< complex<double>* > of double*

Discussion in 'C++' started by J.M., Feb 27, 2007.

  1. J.M.

    J.M. Guest

    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
     
    J.M., Feb 27, 2007
    #1
    1. Advertising

  2. J.M.

    kwikius Guest

    On 27 Feb, 09:47, "J.M." <> wrote:

    > 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
     
    kwikius, Feb 27, 2007
    #2
    1. Advertising

  3. J.M.

    Grizlyk Guest

    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/
     
    Grizlyk, Mar 6, 2007
    #3
  4. J.M.

    Sarath Guest

    On Feb 27, 7:19 pm, "kwikius" <> wrote:
    > On 27 Feb, 09:47, "J.M." <> wrote:
    >
    > > 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?
     
    Sarath, Mar 6, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.amnet.net.au
    Replies:
    1
    Views:
    579
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. Suzanne Vogel
    Replies:
    17
    Views:
    810
    Suzanne Vogel
    Jul 7, 2003
  3. Sydex
    Replies:
    12
    Views:
    6,500
    Victor Bazarov
    Feb 17, 2005
  4. Replies:
    5
    Views:
    432
    James Kanze
    Jun 27, 2008
  5. Alex Vinokur
    Replies:
    1
    Views:
    580
Loading...

Share This Page