convert data type help

Discussion in 'C++' started by Rex_chaos, Oct 8, 2003.

  1. Rex_chaos

    Rex_chaos Guest

    Hi all,
    I have a question about datatype converting. Consider the following
    types
    std::complex<double>
    and
    struct MyComplex
    {
    double re, im;
    }

    I have a larger array of std::complex being converted to that of
    MyComplex

    I have tried std::transform plus a self-defined converting function.
    It works but the efficiency is very low.

    I also tried the following method.

    std::complex<double> *a;
    MyComplex *b;

    //initialize a, b
    b = reinterpret_cast<MyComplex*>(a);

    Yes. Now it's so fast!!! However, I found that the address of a and b
    are the same after casting. That is, one of the allocated memory will
    be lost!!! That's too bad. Please help!
    Rex_chaos, Oct 8, 2003
    #1
    1. Advertising

  2. Rex_chaos

    Attila Feher Guest

    Rex_chaos wrote:
    > Hi all,
    > I have a question about datatype converting. Consider the following
    > types
    > std::complex<double>
    > and
    > struct MyComplex
    > {
    > double re, im;
    > }
    >
    > I have a larger array of std::complex being converted to that of
    > MyComplex
    >
    > I have tried std::transform plus a self-defined converting function.
    > It works but the efficiency is very low.
    >
    > I also tried the following method.
    >
    > std::complex<double> *a;
    > MyComplex *b;
    >
    > //initialize a, b
    > b = reinterpret_cast<MyComplex*>(a);
    >
    > Yes. Now it's so fast!!! However, I found that the address of a and b
    > are the same after casting. That is, one of the allocated memory will
    > be lost!!! That's too bad. Please help!


    I would rather get rid off MyComplex by defining it (using typedef) to be
    std::complex<double>.

    The hack you do with reinterpret cast is at best non-portable, but I would
    say it is so non-portable, that applying a compiler/standard library patch
    might kill it.

    In this case transform is The Right Thing to do. Try to profile the
    transformation (in a simple test program) to see what is slow. If the
    transform algorithm is slow, you might overcome that with a hand-made loop.
    If the actual copying of the elements is slow (or memory allocation for the
    new element) that is unfortunately the trade-off you have to pay for using
    two types.

    --
    Attila aka WW
    Attila Feher, Oct 8, 2003
    #2
    1. Advertising

  3. Rex_chaos

    tom_usenet Guest

    On Wed, 8 Oct 2003 10:31:22 +0300, "Attila Feher"
    <> wrote:

    >The hack you do with reinterpret cast is at best non-portable, but I would
    >say it is so non-portable, that applying a compiler/standard library patch
    >might kill it.


    It is both portable and likely to become standard. See e.g.
    http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2002/n1356.html

    Tom
    tom_usenet, Oct 8, 2003
    #3
  4. Rex_chaos

    tom_usenet Guest

    On Wed, 08 Oct 2003 12:50:56 +0100, tom_usenet
    <> wrote:

    >On Wed, 8 Oct 2003 10:31:22 +0300, "Attila Feher"
    ><> wrote:
    >
    >>The hack you do with reinterpret cast is at best non-portable, but I would
    >>say it is so non-portable, that applying a compiler/standard library patch
    >>might kill it.

    >
    >It is both portable and likely to become standard. See e.g.
    >http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2002/n1356.html


    Actually, scrub that, the reinterpret cast is strictly speaking only
    portable if the cast is to an array of double or the C99 type
    _Complex, although I doubt there is a platform on which it would fail
    to "do the right thing".

    However, if sizeof(MyComplex) == sizeof(double) * 2, then it will
    work, but it isn't yet standard.

    Tom
    tom_usenet, Oct 8, 2003
    #4
  5. Rex_chaos

    tom_usenet Guest

    On 8 Oct 2003 00:20:21 -0700, (Rex_chaos) wrote:

    >Hi all,
    > I have a question about datatype converting. Consider the following
    >types
    >std::complex<double>
    >and
    >struct MyComplex
    >{
    > double re, im;
    >}
    >
    >I have a larger array of std::complex being converted to that of
    >MyComplex
    >
    >I have tried std::transform plus a self-defined converting function.


    You should use a converting functor. e.g.

    struct ComplexConverter
    {
    MyComplex operator()(std::complex<double> const& c) const
    {
    MyComplex mc = {c.real(), c.imag()};
    return mc;
    }
    };

    >It works but the efficiency is very low.


    With the above change, efficiency should be much much higher (when
    compiled with maximum optimization), since the conversion calls will
    be inlined, whereas the calls through a function pointer probably were
    not.

    >
    >I also tried the following method.
    >
    >std::complex<double> *a;
    >MyComplex *b;
    >
    >//initialize a, b
    >b = reinterpret_cast<MyComplex*>(a);
    >
    >Yes. Now it's so fast!!! However, I found that the address of a and b
    >are the same after casting. That is, one of the allocated memory will
    >be lost!!!


    Well, just take a copy of it! e.g.

    MyComplex* sufficientSpace = ...;
    std::copy(b, b + numElements, sufficientSpace);

    > That's too bad. Please help!


    std::complex<double> isn't a POD type, so the cast isn't guaranteed to
    work. However, as long as sizeof(std::complex<double>) ==
    sizeof(MyComplex) == sizeof(double) * 2, it *should* work. Look out
    for the C99 _Complex type, which might help you out.

    Tom
    tom_usenet, Oct 8, 2003
    #5
  6. Rex_chaos

    Attila Feher Guest

    tom_usenet wrote:
    > On Wed, 08 Oct 2003 12:50:56 +0100, tom_usenet
    > <> wrote:
    >
    >> On Wed, 8 Oct 2003 10:31:22 +0300, "Attila Feher"
    >> <> wrote:
    >>
    >>> The hack you do with reinterpret cast is at best non-portable, but
    >>> I would say it is so non-portable, that applying a
    >>> compiler/standard library patch might kill it.

    >>
    >> It is both portable and likely to become standard. See e.g.
    >> http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2002/n1356.html

    >
    > Actually, scrub that, the reinterpret cast is strictly speaking only
    > portable if the cast is to an array of double or the C99 type
    > _Complex, although I doubt there is a platform on which it would fail
    > to "do the right thing".
    >
    > However, if sizeof(MyComplex) == sizeof(double) * 2, then it will
    > work, but it isn't yet standard.


    Ahem. *If* the order of the real/imaginary members is the same!

    --
    Attila aka WW
    Attila Feher, Oct 10, 2003
    #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. luna
    Replies:
    1
    Views:
    6,813
  2. guoqi zheng

    convert type string to type guid

    guoqi zheng, Jul 2, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    13,364
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Jul 2, 2005
  3. Marcin Kalicinski
    Replies:
    11
    Views:
    568
    Niklas Borson
    Feb 13, 2004
  4. Replies:
    6
    Views:
    952
    Greg Comeau
    Oct 20, 2005
  5. Replies:
    14
    Views:
    345
    Tobias Müller
    Jun 1, 2013
Loading...

Share This Page