473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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?

Feb 16 '06 #1
3 2741
In article <11*********************@g47g2000cwa.googlegroups. com>,
"klucar" <kl****@gmail.com> wrote:
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);

--
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.
Feb 16 '06 #2
In article <11*********************@g47g2000cwa.googlegroups. com>,
"klucar" <kl****@gmail.com> wrote:
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.

--
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.
Feb 16 '06 #3
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].

Feb 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Christian Brechbühler | last post by:
The template std::valarray behaves pretty much like a mathematical vector. Arithmetic operators apply elementwise. Now I'd like to extend this to a user-defined type, e.g., complex. ...
2
by: Jim West | last post by:
The GNU implementation of the valarray header (g++ version 3.3.2) includes the comment: // This complication is so to make valarray<valarray<T> > work // even though it is not required by the...
1
by: Steven T. Hatton | last post by:
Examine the following code before you compile and run it. What do you expect to happen? Is what you expected consistent with the result of running it? #include <valarray> #include <iostream> ...
3
by: Peter | last post by:
Hi everybody, I am unfortunately stuck with a probably very simple problem. I made a class called Particle with a valarray (STL) as a class member. The code for the class goes like this: ...
1
by: ES Kim | last post by:
comp.std.c++ would be a better place for this question. Forgive me, but I can't post anything on moderated newsgroups for some reason. valarray doesn't have iterators of its own, which makes...
1
by: ES Kim | last post by:
Here's a code fragment from TC++PL, p679: void f(valarray<double>& v) { size_t i = { 3, 2, 1, 0 }; valarray<size_t> index(i, 4); valarray<double> vv = log(v); } Every compiler I tried...
2
by: Michael Hopkins | last post by:
Hi all I have a subclass of valarray<T> thus template <typename T> class uo_val : public std::valarray<T> { public: uo_val ( ) : std::valarray<T>() {} uo_val (const int sz ) :...
1
by: Oliver Block | last post by:
I have a (9x9) valarray std::valarray<int> va(81); va = 0; // fil all elements with zeros I am taking row slices like that: std::slice_array<int> slr = va; // r = row; a value between 1...
1
by: Dack | last post by:
Hi, I want to track memory leaks in my application (that is using <valarray>). I used the following code: #define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> But then, when I...
5
by: Chris Forone | last post by:
Hello group, g++ (3.4.2, mingw): float init = {1.f, 2.f, 3.f}; std::map<std::string, std::valarray<float mp; mp = std::valarray(init, 3); mp.size(); // should be 3, but IS 0!
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.