is it safe to access a complex<double> array as a double array oftwice the length?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • huili80@gmail.com

    is it safe to access a complex<double> array as a double array oftwice the length?

    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?

    -----------------------------------------
    #include <complex>
    #include <iostream>
    #include <iterator>

    int main()
    {
    using namespace std;

    const size_t n = 5;
    complex<double ca[n];
    double* dp = reinterpret_cas t<double*>(ca) ;

    for (size_t i = 0; i < 2*n; ++i )
    dp[i] = i;

    copy(&ca[0],&ca[n],ostream_iterat or<complex<doub le(cout,", "));
    cout << endl;

    return 0;
    }
  • Gianni Mariani

    #2
    Re: is it safe to access a complex&lt;doub le&gt; array as a double array oftwice the length?

    On Jun 26, 2:04 pm, huil...@gmail.c om wrote:
    Like in the following. Though it gives the expected result with
    gcc4.0, is it really safe to do that?
    No. Not safe.
    What if it's not double but some non-POD type?
    double *is* a POD type. complex<Tis not a POD type.

    It's not guaranteed to work. It may work on your particular
    implementation.

    Comment

    • Christopher Dearlove

      #3
      Re: is it safe to access a complex&lt;doub le&gt; array as a double array of twice the length?

      "James Kanze" <james.kanze@gm ail.comwrote in message
      news:8a7fa8db-8a03-4a8f-b4eb-e90908815a38@a1 g2000hsb.google groups.com...
      In practice, I think you might actually be
      able to risk it (but *only* for complex<floatan d
      complex<double> ).
      What happened to n1589? It's listed as part of "the inspiration or history
      behind
      the active or accepted proposals" in n2566, but I can't see any sign of it
      in n2606.

      Right now I don't need it. But (for Fortran interface) I have needed it in
      the
      past and took the risk indicated (with a reinterpret_cas t) - of course
      testing
      it on the system on which it was used and documenting it. But that's a poor
      substitute for a portable way to do it (which would be without
      reinterpret_cas t
      too).


      Comment

      • Greg Herlihy

        #4
        Re: is it safe to access a complex&lt;doub le&gt; array as a double array oftwice the length?

        On Jun 25, 9:04 pm, huil...@gmail.c om wrote:
        Like in the following. Though it gives the expected result with
        gcc4.0, is it really safe to do that?
        Using a reinterpret_cas t in a program is almost always a red flag -
        and should be interpreted as a warning that the code - if not unsafe -
        is probably fragile.
        #include <complex>
        #include <iostream>
        #include <iterator>
        >
        int main()
        {
                using namespace std;
        >
                const size_t n = 5;
                complex<double ca[n];
                double* dp = reinterpret_cas t<double*>(ca) ;
        >
                for (size_t i = 0; i < 2*n; ++i )
                        dp[i] = i;
        }
        Why not initialize the array of complex numbers in the ordinary (and
        safe) way:

        for (size_t i = 0; i < n; ++i )
        ca[i] = complex<double> (i, i);

        Greg

        Comment

        Working...