By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,708 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,708 IT Pros & Developers. It's quick & easy.

valarray as a class member

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a

"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

P: n/a
"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

P: n/a
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.