469,904 Members | 2,074 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,904 developers. It's quick & easy.

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 2078

"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Christian Brechbühler | last post: by
6 posts views Thread by Steven T. Hatton | last post: by
2 posts views Thread by Boris Sargos | last post: by
1 post views Thread by ES Kim | last post: by
1 post views Thread by Dack | last post: by
2 posts views Thread by Jan Callewaert | last post: by
43 posts views Thread by john | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.