473,406 Members | 2,293 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.

Declaring a valarray, then initialising it later

Jim
Hi,
I want to declare that that a valarray of a certain name exist at the
beginning of some code, but I can't instatiate it until I've read in
some parameters later on in a for loop i.e.

int main(int argc, char * argv[])
{
valarray<floatdata;
float init=0;
for(int i=0; i<argc; i++)
{
//read in parameters, ax1,ax2
valarray<float d(init,ax1*ax2); //***
data=d;
}

}

Problem is when the code leaves the for loop data is of size 0, as
what it's pointed to has been deleted by passing out of scope. I've
tried all sorts of combinations data as extern, defining it outside
the main block, replacing *** with data(init,ax1*ax2); but no success.
If anyone's got any ideas I would be very grateful!
Thanks

Mar 4 '07 #1
9 2115
Jim wrote:
Hi,
I want to declare that that a valarray of a certain name exist at the
beginning of some code, but I can't instatiate it until I've read in
some parameters later on in a for loop i.e.

int main(int argc, char * argv[])
{
valarray<floatdata;
float init=0;
for(int i=0; i<argc; i++)
{
//read in parameters, ax1,ax2
valarray<float d(init,ax1*ax2); //***
data=d;
}

}

Problem is when the code leaves the for loop data is of size 0, as
what it's pointed to has been deleted by passing out of scope. I've
tried all sorts of combinations data as extern, defining it outside
the main block, replacing *** with data(init,ax1*ax2); but no success.
If anyone's got any ideas I would be very grateful!
Thanks
Well I don't quite follow your code (the loop seems misplaced to me) but
why can't you just do this?

int main()
{
float init = 0;
int ax1, ax2;
for (int i = 0; i < argc; ++i)
{
// read in parameters
}
valarray<floatdata(init, ax*ay);
...
}

john
Mar 4 '07 #2
Jim
On Mar 4, 11:08 pm, John Harrison <john_androni...@hotmail.comwrote:
Jim wrote:
Hi,
I want to declare that that a valarray of a certain name exist at the
beginning of some code, but I can't instatiate it until I've read in
some parameters later on in a for loop i.e.
int main(int argc, char * argv[])
{
valarray<floatdata;
float init=0;
for(int i=0; i<argc; i++)
{
//read in parameters, ax1,ax2
valarray<float d(init,ax1*ax2); //***
data=d;
}
}
Problem is when the code leaves the for loop data is of size 0, as
what it's pointed to has been deleted by passing out of scope. I've
tried all sorts of combinations data as extern, defining it outside
the main block, replacing *** with data(init,ax1*ax2); but no success.
If anyone's got any ideas I would be very grateful!
Thanks

Well I don't quite follow your code (the loop seems misplaced to me) but
why can't you just do this?

int main()
{
float init = 0;
int ax1, ax2;
for (int i = 0; i < argc; ++i)
{
// read in parameters
}
valarray<floatdata(init, ax*ay);
...

}

john
This is just an example of one kind of problem I've been having with C+
+. If you know you want a complex variable (class or an array for
instance), you declare it at the start, but then initialise it when
you've got the information to do it. How do you then tie these two
together?

Mar 5 '07 #3
"Jim" ,comp.lang.c++:
I want to declare that that a valarray of a certain name exist at the
beginning of some code, but I can't instatiate it until I've read in
some parameters later on in a for loop i.e.
valarray<floatdata;
<snip>
valarray<float d(init,ax1*ax2); //***
data=d;
It is undefined behaviou to use in an assignment a valarray of a
different size (here, data is a valarray of size 0, while d has size
ax1*ax2). Try use the resize method instead:

data.resize(ax1*ax2,init)
Mar 5 '07 #4
"Jim" ,comp.lang.c++:
I want to declare that that a valarray of a certain name exist at the
beginning of some code, but I can't instatiate it until I've read in
some parameters later on in a for loop i.e.
valarray<floatdata;
<snip>
valarray<float d(init,ax1*ax2); //***
data=d;
It is undefined behaviour to use in an assignment a valarray of a
different size (here, data is a valarray of size 0, while d has size
ax1*ax2). Try using the resize method instead:

data.resize(ax1*ax2,init)

Mar 5 '07 #5
Jim wrote:
On Mar 4, 11:08 pm, John Harrison <john_androni...@hotmail.comwrote:
>>Jim wrote:
>>>Hi,
I want to declare that that a valarray of a certain name exist at the
beginning of some code, but I can't instatiate it until I've read in
some parameters later on in a for loop i.e.
>>>int main(int argc, char * argv[])
{
valarray<floatdata;
float init=0;
for(int i=0; i<argc; i++)
{
//read in parameters, ax1,ax2
valarray<float d(init,ax1*ax2); //***
data=d;
}
>>>}
>>>Problem is when the code leaves the for loop data is of size 0, as
what it's pointed to has been deleted by passing out of scope. I've
tried all sorts of combinations data as extern, defining it outside
the main block, replacing *** with data(init,ax1*ax2); but no success.
If anyone's got any ideas I would be very grateful!
Thanks

Well I don't quite follow your code (the loop seems misplaced to me) but
why can't you just do this?

int main()
{
float init = 0;
int ax1, ax2;
for (int i = 0; i < argc; ++i)
{
// read in parameters
}
valarray<floatdata(init, ax*ay);
...

}

john


This is just an example of one kind of problem I've been having with C+
+. If you know you want a complex variable (class or an array for
instance), you declare it at the start, but then initialise it when
you've got the information to do it. How do you then tie these two
together?
Don't declare it at the start, declare it only when you have the
necessary information to initialise it. I feel I must be missing something.

john
Mar 5 '07 #6
Jim
On Mar 5, 5:59 pm, John Harrison <john_androni...@hotmail.comwrote:
Jim wrote:
On Mar 4, 11:08 pm, John Harrison <john_androni...@hotmail.comwrote:
>Jim wrote:
>>Hi,
I want to declare that that a valarray of a certain name exist at the
beginning of some code, but I can't instatiate it until I've read in
some parameters later on in a for loop i.e.
>>int main(int argc, char * argv[])
{
valarray<floatdata;
float init=0;
for(int i=0; i<argc; i++)
{
//read in parameters, ax1,ax2
valarray<float d(init,ax1*ax2); //***
data=d;
}
>>}
>>Problem is when the code leaves the for loop data is of size 0, as
what it's pointed to has been deleted by passing out of scope. I've
tried all sorts of combinations data as extern, defining it outside
the main block, replacing *** with data(init,ax1*ax2); but no success.
If anyone's got any ideas I would be very grateful!
Thanks
>Well I don't quite follow your code (the loop seems misplaced to me) but
why can't you just do this?
>int main()
{
float init = 0;
int ax1, ax2;
for (int i = 0; i < argc; ++i)
{
// read in parameters
}
valarray<floatdata(init, ax*ay);
...
>}
>john
This is just an example of one kind of problem I've been having with C+
+. If you know you want a complex variable (class or an array for
instance), you declare it at the start, but then initialise it when
you've got the information to do it. How do you then tie these two
together?

Don't declare it at the start, declare it only when you have the
necessary information to initialise it. I feel I must be missing something.

john

Ok, if you have a class, all of the member variables are declared in
the class definition, and are then available to all of the methods in
that class. I want to have an object with a valarray in it, it's got
to be a valarray as unfortunately the code written by someone else
supplies a valarray. Copying it to a different type would be
inefficient. In the constructor I read in the file using a supplied
method which when given an uninitialised valarray (i.e.
valarray<floatd;) gets given a set of values and a confirmed length
etc. Even if I declare a valarray in the method, or use the one in the
class definition the size of the valarray is what it should be whilst
in the method, but as soon as you leave the scope of that method the
array reverts to zero, meaning it's useless to the rest of the class.
I can't find any way of initialising a declared valarray.

Mar 5 '07 #7
Jim
On Mar 5, 3:47 pm, Pierre Senellart <inva...@invalid.invalidwrote:
"Jim" ,comp.lang.c++:
I want to declare that that a valarray of a certain name exist at the
beginning of some code, but I can't instatiate it until I've read in
some parameters later on in a for loop i.e.
valarray<floatdata;
<snip>
valarray<float d(init,ax1*ax2); //***
data=d;

It is undefined behaviour to use in an assignment a valarray of a
different size (here, data is a valarray of size 0, while d has size
ax1*ax2). Try using the resize method instead:

data.resize(ax1*ax2,init)
I've just tried that and unfortunately it failed, data went back to
it's original size.

Mar 5 '07 #8
Jim wrote:
On Mar 5, 5:59 pm, John Harrison <john_androni...@hotmail.comwrote:
>>Jim wrote:
>>>On Mar 4, 11:08 pm, John Harrison <john_androni...@hotmail.comwrote:
>>>>Jim wrote:
>>>>>Hi,
>I want to declare that that a valarray of a certain name exist at the
>beginning of some code, but I can't instatiate it until I've read in
>some parameters later on in a for loop i.e.
>>>>>int main(int argc, char * argv[])
>{
valarray<floatdata;
float init=0;
for(int i=0; i<argc; i++)
{
//read in parameters, ax1,ax2
valarray<float d(init,ax1*ax2); //***
data=d;
}
>>>>>}
>>>>>Problem is when the code leaves the for loop data is of size 0, as
>what it's pointed to has been deleted by passing out of scope. I've
>tried all sorts of combinations data as extern, defining it outside
>the main block, replacing *** with data(init,ax1*ax2); but no success.
>If anyone's got any ideas I would be very grateful!
>Thanks
>>>>Well I don't quite follow your code (the loop seems misplaced to me) but
why can't you just do this?
>>>>int main()
{
float init = 0;
int ax1, ax2;
for (int i = 0; i < argc; ++i)
{
// read in parameters
}
valarray<floatdata(init, ax*ay);
...
>>>>}
>>>>john
>>>This is just an example of one kind of problem I've been having with C+
+. If you know you want a complex variable (class or an array for
instance), you declare it at the start, but then initialise it when
you've got the information to do it. How do you then tie these two
together?

Don't declare it at the start, declare it only when you have the
necessary information to initialise it. I feel I must be missing something.

john

Ok, if you have a class, all of the member variables are declared in
the class definition, and are then available to all of the methods in
that class. I want to have an object with a valarray in it, it's got
to be a valarray as unfortunately the code written by someone else
supplies a valarray. Copying it to a different type would be
inefficient. In the constructor I read in the file using a supplied
method which when given an uninitialised valarray (i.e.
valarray<floatd;) gets given a set of values and a confirmed length
etc. Even if I declare a valarray in the method, or use the one in the
class definition the size of the valarray is what it should be whilst
in the method, but as soon as you leave the scope of that method the
array reverts to zero, meaning it's useless to the rest of the class.
I can't find any way of initialising a declared valarray.
Well I feel you must be making a mistake somewhere in your code. This
shouldn't difficult. Why not post the actual code?

john
Mar 5 '07 #9
>>
>>
Ok, if you have a class, all of the member variables are declared in
the class definition, and are then available to all of the methods in
that class. I want to have an object with a valarray in it, it's got
to be a valarray as unfortunately the code written by someone else
supplies a valarray. Copying it to a different type would be
inefficient. In the constructor I read in the file using a supplied
method which when given an uninitialised valarray (i.e.
valarray<floatd;) gets given a set of values and a confirmed length
etc. Even if I declare a valarray in the method, or use the one in the
class definition the size of the valarray is what it should be whilst
in the method, but as soon as you leave the scope of that method the
array reverts to zero, meaning it's useless to the rest of the class.
I can't find any way of initialising a declared valarray.

Well I feel you must be making a mistake somewhere in your code. This
shouldn't difficult. Why not post the actual code?

john
For instance this program print 0, 100, 100. The size of data has been
sucessfully changed.

#include <iostream>
#include <valarray>

class A
{
public:
A();
std::valarray<floatdata;
};

A::A()
{
std::cout << data.size() << '\n';
std::valarray<floatd(1.0, 100);
data.resize(100);
data = d;
std::cout << data.size() << '\n';
}

int main()
{
A a;
std::cout << a.data.size() << '\n';
}
Mar 5 '07 #10

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. ...
6
by: Steven T. Hatton | last post by:
I bought Josuttis's book on the repeated recommendations of people in this newsgroup. http://www.josuttis.com/libbook/ One of the first things I looked up was the std::valarray<>. And what I...
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...
2
by: Sanyi | last post by:
Hello, I've did some some numerical programs in C/C++ before, but decided to use STL to write neater code. Then again I don't want to give up speed so I have a couple of questions. Apparently...
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: 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...
2
by: john | last post by:
Hi, in TC++PL3 on page 665, regarding valarray member functions, it is mentioned: "valarray operator-() const; // result= -v for every element // similarly: +, ~, !" I checked the web and...
43
by: john | last post by:
Hi, in TC++PL 3 on pages 674-675 it is mentioned: "Maybe your first idea for a two-dimensional vector was something like this: class Matrix { valarray< valarray<doublev; public: // ... };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.