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. 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
"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. 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
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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.
...
|
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...
|
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 :
...
|
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...
|
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...
|
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>() {}...
|
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...
|
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...
|
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:
// ...
};
|
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...
|
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...
|
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...
|
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...
|
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...
|
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++...
|
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...
|
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...
|
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...
| |