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

Discussion in 'C++' started by huili80@gmail.com, Jun 26, 2008.

  1. Guest

    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_cast<double*>(ca);

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

    copy(&ca[0],&ca[n],ostream_iterator<complex<double> >(cout,", "));
    cout << endl;

    return 0;
    }
    , Jun 26, 2008
    #1
    1. Advertising

  2. On Jun 26, 2:04 pm, 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<T> is not a POD type.

    It's not guaranteed to work. It may work on your particular
    implementation.
    Gianni Mariani, Jun 26, 2008
    #2
    1. Advertising

  3. James Kanze Guest

    On Jun 26, 7:05 am, Gianni Mariani <> wrote:
    > On Jun 26, 2:04 pm, wrote:


    > > Like in the following. Though it gives the expected result with
    > > gcc4.0, is it really safe to do that?


    > No. Not safe.


    Officially.

    > > What if it's not double but some non-POD type?


    > double *is* a POD type. complex<T> is not a POD type.


    I think he was asking about something like complex< BigNumber >.

    Of course, "The effect of instantiating the template complex for
    any type other than float, double, or long double is
    unspecified" (§26.3), so the question is moot.

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


    There are in fact a number of very strong motiviations for an
    implementation to make it work. It's guaranteed in Fortran, and
    as an implementer, you want your complex to be compatible with
    those of Fortran. In practice, I think you might actually be
    able to risk it (but *only* for complex<float> and
    complex<double>).

    (I'm presuming that the reason the poster wants to do this is in
    order to interface with some legacy code. Otherwise, I'd
    consider it very bad practice, even if it were formally
    guaranteed.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 26, 2008
    #3
  4. Re: is it safe to access a complex<double> array as a double array of twice the length?

    "James Kanze" <> wrote in message
    news:...
    > In practice, I think you might actually be
    > able to risk it (but *only* for complex<float> and
    > 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_cast) - 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_cast
    too).
    Christopher Dearlove, Jun 26, 2008
    #4
  5. Greg Herlihy Guest

    On Jun 25, 9:04 pm, wrote:
    > Like in the following. Though it gives the expected result with
    > gcc4.0, is it really safe to do that?


    Using a reinterpret_cast 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_cast<double*>(ca);
    >
    >         for (size_t i = 0; i < 2*n; ++i )
    >                 dp = i;
    > }


    Why not initialize the array of complex numbers in the ordinary (and
    safe) way:

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

    Greg
    Greg Herlihy, Jun 27, 2008
    #5
  6. James Kanze Guest

    On Jun 26, 6:04 am, wrote:
    > Like in the following. Though it gives the expected result
    > with gcc4.0, is it really safe to do that?


    Pragmatically, I think yes, although it's not something I'd like
    to see in code unless absolutely necessary. (Typically, I'd see
    it being used to cast the address of an array of complex to a
    double* in order to pass it to some legacy code. Or Fortran.
    Or C.)

    > What if it's not double but some non-POD type?


    What if what isn't double? std::complex is defined for float,
    double and long double, and that's it. Anything beyond that is
    implementation defined, if it's defined at all.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 27, 2008
    #6
    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:
    572
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. Stanimir Stamenkov
    Replies:
    2
    Views:
    743
    Stanimir Stamenkov
    Oct 25, 2005
  3. Sydex
    Replies:
    12
    Views:
    6,483
    Victor Bazarov
    Feb 17, 2005
  4. Replies:
    2
    Views:
    565
    Victor Bazarov
    May 20, 2006
  5. J.M.
    Replies:
    3
    Views:
    628
    Sarath
    Mar 6, 2007
Loading...

Share This Page