valarray construction from C-array

K

klucar

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?
 
D

Daniel T.

"klucar said:
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?

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

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

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

Daniel T.

"klucar said:
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?

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

klucar

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].
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top