valarray construction from C-array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • klucar

    #1

    valarray construction from C-array

    I'll start off differently by showing what I know how to do:

    valarray<float> va(100, 0.0f);
    float* fp;
    fp = &va[0];
    some_c_function ( fp, va.size() );

    What I want to do though is quite the opposite:

    float* fp;
    valarray<float> va;

    &va[0] = fp; // this doesn't work on many levels

    What I am trying to show is that I want to construct a valarray, but
    use some pointer (from shared memory for example) to initialize the
    valarray. I want to overlay a valarray on a c-style array so I don't
    suffer the penalty of copying all the data from the array like the
    following constructor does:

    float af1 [] = {0, 1, 2, 3};
    valarray <float> vf1 (af1, 4);

    Is this possible with just STL or do I need to dive in to some other
    C++ numerics library?

  • Daniel T.

    #2
    Re: valarray construction from C-array

    In article <1140119297.863 788.37060@g47g2 000cwa.googlegr oups.com>,
    "klucar" <klucar@gmail.c om> wrote:
    [color=blue]
    > I'll start off differently by showing what I know how to do:
    >
    > valarray<float> va(100, 0.0f);
    > float* fp;
    > fp = &va[0];
    > some_c_function ( fp, va.size() );
    >
    > What I want to do though is quite the opposite:
    >
    > float* fp;
    > valarray<float> va;
    >
    > &va[0] = fp; // this doesn't work on many levels
    >
    > What I am trying to show is that I want to construct a valarray, but
    > use some pointer (from shared memory for example) to initialize the
    > valarray. I want to overlay a valarray on a c-style array so I don't
    > suffer the penalty of copying all the data from the array like the
    > following constructor does:
    >
    > float af1 [] = {0, 1, 2, 3};
    > valarray <float> vf1 (af1, 4);
    >
    > Is this possible with just STL or do I need to dive in to some other
    > C++ numerics library?[/color]

    int array[] = { 3, 6, 18, 3, 22 };

    // initialize valarray by elements of an ordinary array
    std::valarray<i nt> va3(array, sizeof (array)/sizeof (array[0]));

    // initialize by the second to the fourth element
    std::valarray<i nt> va4(array+1, 3);

    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.

    Comment

    • Daniel T.

      #3
      Re: valarray construction from C-array

      In article <1140119297.863 788.37060@g47g2 000cwa.googlegr oups.com>,
      "klucar" <klucar@gmail.c om> wrote:
      [color=blue]
      > I'll start off differently by showing what I know how to do:
      >
      > valarray<float> va(100, 0.0f);
      > float* fp;
      > fp = &va[0];
      > some_c_function ( fp, va.size() );
      >
      > What I want to do though is quite the opposite:
      >
      > float* fp;
      > valarray<float> va;
      >
      > &va[0] = fp; // this doesn't work on many levels
      >
      > What I am trying to show is that I want to construct a valarray, but
      > use some pointer (from shared memory for example) to initialize the
      > valarray. I want to overlay a valarray on a c-style array so I don't
      > suffer the penalty of copying all the data from the array like the
      > following constructor does:
      >
      > float af1 [] = {0, 1, 2, 3};
      > valarray <float> vf1 (af1, 4);
      >
      > Is this possible with just STL or do I need to dive in to some other
      > C++ numerics library?[/color]

      Sorry, my last post was premature. valarray holds its data by value just
      like other containers.

      --
      Magic depends on tradition and belief. It does not welcome observation,
      nor does it profit by experiment. On the other hand, science is based
      on experience; it is open to correction by observation and experiment.

      Comment

      • klucar

        #4
        Re: valarray construction from C-array

        So is my only hope to get a pointer to a valarray object? It just
        seems bad that C++ containers have to be in charge. I guess a memory
        manager object would have to use valarrays to store data and then pass
        out valarray pointers to C++ objects and C pointers to C functions
        using &va[0].

        Comment

        Working...