473,387 Members | 1,549 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,387 software developers and data experts.

how to best use valarrays as constructor arguments

Presently I have some difficulties with the following type of code which
is understandably causing a segfault:

//---- begin code ----

#include <valarray>
#include <iostream>
using namespace std;
class SomeClass {

private:
valarray <doublemx;

public:
SomeClass(valarray <double>& x) {mx = x;}
void func() {cout << mx[1];} // <---- Segfault here.

};
int main () {
valarray <doublex(5,10);
SomeClass foo(x);

foo.func();

return 0;
}

//---- end code ----

It will fail at runtime because, as I understand it, mx is a zero-size
valarray and hence mx[1] does not exist.

Yet (for a simple numerical code) I would like something similar to the
above to work, i.e. I'd like to pass a valarray of predefined size to
the constructor of a class to initialize a class-member valarray, say
mx, and after reference to mx's elements as mx[0], mx[1] etc. As a C++
starter I'm confused about the best way to work around the problem in
the code above. Any advice would be welcome.

Regards,
Gerard.
Jul 10 '06 #1
9 1718
Gerard Kramer wrote:
Presently I have some difficulties with the following type of code
which is understandably causing a segfault:

//---- begin code ----

#include <valarray>
#include <iostream>
using namespace std;
class SomeClass {

private:
valarray <doublemx;

public:
SomeClass(valarray <double>& x) {mx = x;}
void func() {cout << mx[1];} // <---- Segfault here.
Really?!!
>
};
int main () {
valarray <doublex(5,10);
SomeClass foo(x);

foo.func();

return 0;
}

//---- end code ----

It will fail at runtime because, as I understand it, mx is a zero-size
valarray and hence mx[1] does not exist.
It *will* fail or it *does* fail?

With the exception of using assignment instead of initialisation and some
superfluousness and extraneous whitespace, your code is fine, AFAICT.
Yet (for a simple numerical code) I would like something similar to
the above to work, i.e. I'd like to pass a valarray of predefined
size to the constructor of a class to initialize a class-member
valarray, say mx, and after reference to mx's elements as mx[0],
mx[1] etc. As a C++ starter I'm confused about the best way to work
around the problem in the code above. Any advice would be welcome.
What problem? Prove to us there is a problem, post the results of your
run, not your speculations. Step through it in a debugger. Print out
the *size* of 'mx' instead of its element. Anyway, do something, don't
just stand there.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 10 '06 #2
Victor Bazarov wrote:
Gerard Kramer wrote:
>Presently I have some difficulties with the following type of code
which is understandably causing a segfault:

//---- begin code ----

#include <valarray>
#include <iostream>
using namespace std;
class SomeClass {

private:
valarray <doublemx;

public:
SomeClass(valarray <double>& x) {mx = x;}
void func() {cout << mx[1];} // <---- Segfault here.

Really?!!
yes ;-)
>};
int main () {
valarray <doublex(5,10);
SomeClass foo(x);

foo.func();

return 0;
}

//---- end code ----

It will fail at runtime because, as I understand it, mx is a zero-size
valarray and hence mx[1] does not exist.

It *will* fail or it *does* fail?
It *will* fail (by looking at the code) and so it *does* (by compiling
and running it.)
>
With the exception of using assignment instead of initialisation and some
superfluousness and extraneous whitespace, your code is fine, AFAICT.
>Yet (for a simple numerical code) I would like something similar to
the above to work, i.e. I'd like to pass a valarray of predefined
size to the constructor of a class to initialize a class-member
valarray, say mx, and after reference to mx's elements as mx[0],
mx[1] etc. As a C++ starter I'm confused about the best way to work
around the problem in the code above. Any advice would be welcome.

What problem? Prove to us there is a problem, post the results of your
run, not your speculations. Step through it in a debugger. Print out
the *size* of 'mx' instead of its element. Anyway, do something, don't
just stand there.
I'm sorry? Well, thanks for the reply. As far as I can see I didn't
speculate about anything. Just to be clear: There is no problem, but I'd
like to learn how to get a certain thing done, namely:

The best way to somehow to use a valarray <doubleas an argument of a
class constructor to initialize a class member variable of type valarray
<double>.

I can't see how to do that.

That's all. I'm sorry in case that bothered you. Won't happen again.
>
V
Jul 10 '06 #3
In message <e8**********@localhost.localdomain>, Gerard Kramer
<re**********@usenet.comwrites
>Victor Bazarov wrote:
>Gerard Kramer wrote:
>>Presently I have some difficulties with the following type of code
which is understandably causing a segfault:

//---- begin code ----

#include <valarray>
#include <iostream>
using namespace std;
class SomeClass {

private:
valarray <doublemx;

public:
SomeClass(valarray <double>& x)
I'd recommend making this take a const reference, but it shouldn't make
any difference to the rest of the code.
>>>{mx = x;}
.... and I'd recommend using an initializer list, but again it shouldn't
make any difference.

: mx(x) {}
>> void func() {cout << mx[1];} // <---- Segfault here.
.... and this really ought to be a const member function, but again it
shouldn't matter.
>>
Really?!!

yes ;-)
>>};
int main () {
valarray <doublex(5,10);
SomeClass foo(x);

foo.func();

return 0;
}

//---- end code ----

It will fail at runtime because, as I understand it, mx is a zero-size
valarray and hence mx[1] does not exist.

It *will* fail or it *does* fail?

It *will* fail (by looking at the code)
Can you describe, line by line, what you think the code is doing?
and so it *does* (by compiling
and running it.)
>>
With the exception of using assignment instead of initialisation and some
superfluousness and extraneous whitespace, your code is fine, AFAICT.
>>Yet (for a simple numerical code) I would like something similar to
the above to work, i.e. I'd like to pass a valarray of predefined
size to the constructor of a class to initialize a class-member
valarray, say mx, and after reference to mx's elements as mx[0],
mx[1] etc. As a C++ starter I'm confused about the best way to work
around the problem in the code above. Any advice would be welcome.

What problem? Prove to us there is a problem, post the results of your
run, not your speculations. Step through it in a debugger. Print out
the *size* of 'mx' instead of its element. Anyway, do something, don't
just stand there.

I'm sorry? Well, thanks for the reply. As far as I can see I didn't
speculate about anything. Just to be clear: There is no problem, but I'd
like to learn how to get a certain thing done, namely:

The best way to somehow to use a valarray <doubleas an argument of a
class constructor to initialize a class member variable of type valarray
<double>.
SomeClass(valarray <doubleconst & x) : mx(x) {}
>
I can't see how to do that.

That's all. I'm sorry in case that bothered you. Won't happen again.
>>
V
--
Richard Herring
Jul 10 '06 #4

Gerard Kramer wrote:
Presently I have some difficulties with the following type of code which
is understandably causing a segfault:

//---- begin code ----

#include <valarray>
#include <iostream>
using namespace std;
class SomeClass {

private:
valarray <doublemx;

public:
SomeClass(valarray <double>& x) {mx = x;}
The above has undefined behaviour. Once you enter the constructor, mx
has size 0, but you are assigning to it a valarray of size != 0, which
isn't allowed. I think you meant:

SomeClass(valarray<doubleconst& x): mx(x) {}
void func() {cout << mx[1];} // <---- Segfault here.
With that change, no segfault will happen.
>
};
int main () {
valarray <doublex(5,10);
SomeClass foo(x);

foo.func();

return 0;
}

//---- end code ----

It will fail at runtime because, as I understand it, mx is a zero-size
valarray and hence mx[1] does not exist.
Well, the bug occurs before func() is even called.
Yet (for a simple numerical code) I would like something similar to the
above to work, i.e. I'd like to pass a valarray of predefined size to
the constructor of a class to initialize a class-member valarray, say
mx, and after reference to mx's elements as mx[0], mx[1] etc. As a C++
starter I'm confused about the best way to work around the problem in
the code above. Any advice would be welcome.
Just copy the valarray, rather than assigning it. If you must assign,
manually set the size first:
SomeClass(valarray <doubleconst& x) {
mx.resize(x.size());
mx = x;
}

As a general rule, avoid valarray entirely. There are far superior
maths libraries out there, such as boost::ublas or blitz++.

Tom

Jul 10 '06 #5
Gerard Kramer wrote:
Victor Bazarov wrote:
>Gerard Kramer wrote:
>>Presently I have some difficulties with the following type of code
which is understandably causing a segfault:
[..]

It will fail at runtime because, as I understand it, mx is a
zero-size valarray and hence mx[1] does not exist.

It *will* fail or it *does* fail?

It *will* fail (by looking at the code) and so it *does* (by compiling
and running it.)
The reason I asked was that I took your code, compiled it and ran it,
and it ran fine on VC++ 2005.
>With the exception of using assignment instead of initialisation and
some superfluousness and extraneous whitespace, your code is fine,
AFAICT.
>>Yet (for a simple numerical code) I would like something similar to
the above to work, i.e. I'd like to pass a valarray of predefined
size to the constructor of a class to initialize a class-member
valarray, say mx, and after reference to mx's elements as mx[0],
mx[1] etc. As a C++ starter I'm confused about the best way to work
around the problem in the code above. Any advice would be welcome.

What problem? Prove to us there is a problem, post the results of
your run, not your speculations. Step through it in a debugger.
Print out the *size* of 'mx' instead of its element. Anyway, do
something, don't just stand there.

I'm sorry? Well, thanks for the reply. As far as I can see I didn't
speculate about anything.
You said "it will fail". Any statement about the future is speculation
by definition. Don't take offence, it just is.

Then you said 'mx is a zero-size valarray'. Have you actually looked?
Have you tried running it under a debugger? Have you tried printing out
the size of the 'mx' valarray?
Just to be clear: There is no problem, but
I am sorry. No problem?
I'd like to learn how to get a certain thing done, namely:

The best way to somehow to use a valarray <doubleas an argument of a
class constructor to initialize a class member variable of type
valarray <double>.

I can't see how to do that.
But you *did* that. Aside from small things that don't matter in this
particular case, your code is [supposed to be] working.
That's all. I'm sorry in case that bothered you. Won't happen again.
Nothing bothered me. Nothing to apologise for. You made some statements
that prompted me to ask more questions. Just answer them.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 10 '06 #6
Tom Widmer wrote:
Gerard Kramer wrote:
>Presently I have some difficulties with the following type of code
which is understandably causing a segfault:

//---- begin code ----

#include <valarray>
#include <iostream>
using namespace std;
class SomeClass {

private:
valarray <doublemx;

public:
SomeClass(valarray <double>& x) {mx = x;}

The above has undefined behaviour. Once you enter the constructor, mx
has size 0, but you are assigning to it a valarray of size != 0, which
isn't allowed. [..]
Alright, it was my mistake to have forgotten about this. When I took the
code, I did the change and didn't think twice about it.

Gerard, I am sorry for having missed this particular part in your code.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 10 '06 #7
Victor Bazarov wrote:
Gerard Kramer wrote:
>Victor Bazarov wrote:
>>Gerard Kramer wrote:
Presently I have some difficulties with the following type of code
which is understandably causing a segfault:
[..]

It will fail at runtime because, as I understand it, mx is a
zero-size valarray and hence mx[1] does not exist.

It *will* fail or it *does* fail?

It *will* fail (by looking at the code) and so it *does* (by
compiling and running it.)

The reason I asked was that I took your code, compiled it and ran it,
and it ran fine on VC++ 2005.
Sorry, this just shows that undefined behaviour is just that, undefined,
and compiling and running is not necessarily the proof of correctness.

I still stand by the advice to step through it in a debugger.
[...]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 10 '06 #8
In message <JO**************@baesystems.com>, I wrote
>In message <e8**********@localhost.localdomain>, Gerard Kramer
<re**********@usenet.comwrites
>>Victor Bazarov wrote:
>>Gerard Kramer wrote:
Presently I have some difficulties with the following type of code
which is understandably causing a segfault:

//---- begin code ----

#include <valarray>
#include <iostream>
using namespace std;
class SomeClass {

private:
valarray <doublemx;

public:
SomeClass(valarray <double>& x)

I'd recommend making this take a const reference, but it shouldn't make
any difference to the rest of the code.
>>>>{mx = x;}

... and I'd recommend using an initializer list, but again it shouldn't
make any difference.

: mx(x) {}
Oops. That certainly does make a difference. Instead of checking the
standard I looked at an old document which wrongly implied that
assignment resizes.

--
Richard Herring
Jul 10 '06 #9
Gerard Kramer wrote:
Presently I have some difficulties with the following type of code which
is understandably causing a segfault:

//---- begin code ----

#include <valarray>
#include <iostream>
using namespace std;
class SomeClass {

private:
valarray <doublemx;

public:
SomeClass(valarray <double>& x) {mx = x;}
void func() {cout << mx[1];} // <---- Segfault here.

};
int main () {
valarray <doublex(5,10);
SomeClass foo(x);

foo.func();

return 0;
}

//---- end code ----

It will fail at runtime because, as I understand it, mx is a zero-size
valarray and hence mx[1] does not exist.

Yet (for a simple numerical code) I would like something similar to the
above to work, i.e. I'd like to pass a valarray of predefined size to
the constructor of a class to initialize a class-member valarray, say
mx, and after reference to mx's elements as mx[0], mx[1] etc. As a C++
starter I'm confused about the best way to work around the problem in
the code above. Any advice would be welcome.

Regards,
Gerard.
Thank you all very much for your replies. (Including Victor's ;-) I
understand the solution involving an initializer list. For some reason
my C++ notes didn't mention this "construction" but strangle claimed
that assignment should do the job.

I also took note of Tom Widmer's remark. Probably you're right about
Blitz and others outperforming the valarray template class. Actually, I
did install Blitz on my machine, but only some time after encountering
valarray, which kept me wondering how to make my original code work.

Regards,
Gerard.

Jul 10 '06 #10

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

Similar topics

10
by: Michael Aramini | last post by:
I need to represent 1D and 2D arrays of numeric or bool types in a C++ program. The sizes of the arrays in my intended application are dynamic in the sense that they are not known at compile time,...
9
by: Matt Eberts | last post by:
Sorry, bad title. Anyway, is there a way to pass the arguments to an object instantiated via a constructor using the arguments object and have it expanded, so to speak, so that it doesn't appear as...
11
by: The Directive | last post by:
This code will not compiled: In main function: Dot temp = new Dot( *(new Point( 5, 5 )) ); In Dot class: //Constructor with default arguments. Dot::Dot( Point& point= *(new Point( 5, 5...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
24
by: slurper | last post by:
i have the following class sequence { public: sequence (const sequence& mysequence, const int newjob) { job_sequence(mysequence.job_sequence) job_sequence.push_back(newjob); ... }
5
by: askmeofit | last post by:
I'm a Phd student in statistics, and I'm writing computer code for the simulation of stochastic differential equations. Little background about my knowledge: I know C programming decently, and my...
7
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id -...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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:
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
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
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...

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.