472,354 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

valarray as a class member

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:

class Particle {
private:
valarray<double>* position;
public:
Particle() {position = new valarray<double>(3);}
~Particle() {delete[] position;}

const valarray<double>& getPosition() const {
return *position;
}

} ;
//Testing the class Particle
int main(int argc, char *argv[])
{

Particle a;
valarray<double> temp;
temp = a.getPosition();
cout<<temp[1]<<endl;
system("PAUSE");
return 0;
}
This code snippet does compile. But when I run the program I get
segmentation fault and cannot figure out why.

My questions are:
* How do i have to correct the above code to avoid the segmentation
fault?
Important is, that I want the position vector to be a member
variable of the
class Particle. Do I always need a pointer for this or can I
declare the member variable in the class like in Java:
valarray<double> position;

* is the getter Method getPosition() correct?
Every help from anybody is greatly appreciated!
Don't be confused by my English, i am not a native speaker ;-)

Peter.
Jul 22 '05 #1
3 2186

"Peter" <pe***********@yahoo.de> wrote in message
news:19**************************@posting.google.c om...
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:

class Particle {
private:
valarray<double>* position;
public:
Particle() {position = new valarray<double>(3);}
~Particle() {delete[] position;}
Omit the square bracket. Ordinary delete is what you want.
const valarray<double>& getPosition() const {
return *position;
}

} ;


Also, the compiler-generated copy constructor probably isn't what you want. It's
best to make your class noncopyable, add a cloning copy constructor, or use
reference counting.

Jonathan
Jul 22 '05 #2
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message news:<30*************@uni-berlin.de>...
"Peter" <pe***********@yahoo.de> wrote in message
news:19**************************@posting.google.c om...
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:

class Particle {
private:
valarray<double>* position;
public:
Particle() {position = new valarray<double>(3);}
~Particle() {delete[] position;}
Omit the square bracket. Ordinary delete is what you want.


Yes you are absolutely right, since I do not allocate with new[];

const valarray<double>& getPosition() const {
return *position;
}

} ;


Also, the compiler-generated copy constructor probably isn't what you want. It's
best to make your class noncopyable, add a cloning copy constructor, or use
reference counting.

Also a good point. Thanks. Jonathan


Do you have an idea why i get the segmentation fault when running the code?
I think the problem lies in the implementation of the constructor.
I am not sure if position = new valarray<double>(3) is the correct way to
do it.

Thank you for your fast response!

Best wishes, Robert.
Jul 22 '05 #3
pe***********@yahoo.de (Peter) wrote

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:

class Particle {
private:
valarray<double>* position;
public:
Particle() {position = new valarray<double>(3);}
~Particle() {delete[] position;}
That should be: delete position;
Only use delete[] when you used new[]

const valarray<double>& getPosition() const {
return *position;
}
} ;
There is no valarray member in that class. There is a pointer
to valarray (these are two very different things).

Is there some reason you didn't make it a valarray member?
That would have solved all of your problems.
//Testing the class Particle
int main(int argc, char *argv[])
{

Particle a;
valarray<double> temp;
temp = a.getPosition();
At this point you have 2 pointers to valarray: the one in a,
and the one in temp. They both point to the same valarray
in memory.
cout<<temp[1]<<endl;
system("PAUSE");
return 0;
}


Now a and temp both get deleted. You deleted the same memory
twice so you get a segfault. To fix this, you need to add
a copy-constructor to your class, which will allocate a
new valarray. For example (in the class):

Particle(Particle const &p) {
position = new valarray<double>(3);
// code to copy values from p.position into *position
}
Jul 22 '05 #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. ...
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...
2
by: Boris Sargos | last post by:
Hi, I wrote the Matrix class suggested by Stroustrup in his reference book, and in this aim, wrote too the following member function, which returns the ith row of the Matrix : ...
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: 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: Jan Callewaert | last post by:
Hi, since I want to specify an extra function for a std::valarray<float>, I want to subclass it: class FVector : public std::valarray<float> { public: FVector() : std::valarray<float>() {}...
9
by: Jim | last post by:
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...
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: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.