473,715 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2279

"Peter" <pe***********@ yahoo.de> wrote in message
news:19******** *************** ***@posting.goo gle.com...
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******@kanga roologic.com> wrote in message news:<30******* ******@uni-berlin.de>...
"Peter" <pe***********@ yahoo.de> wrote in message
news:19******** *************** ***@posting.goo gle.com...
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***********@y ahoo.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(Partic le 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
3662
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. Multiplying a double by a complex number gives a complex number. Now I would like to multiply a valarray of doubles by a complex number and get a valarray of complex numbers. Of course, a C++ compiler won't jump to that conclusion; I need to define...
6
4325
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 read is that he questions the worth of the valarray. http://www.cs.bsu.edu/homepages/peb/cplusplus/headers/valarray.htm
2
3827
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 : std::valarray<double> MyMatrix::Row ( int i) const { return ( ValArarray ) ; }
1
1790
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 functions in <algorithm> less useful. &v works like an iterator, but even this trick doesn't work for const valarray's. void f(const valarray<double>& v) {
1
2569
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 include <valarray> the compiler raises the following errors :
2
2399
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>() {} FVector(size_t size) : std::valarray<float>(size) {} FVector(const FVector & other) : std::valarray<float>(other) {}
9
2136
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 main(int argc, char * argv) { valarray<floatdata; float init=0; for(int i=0; i<argc; i++)
2
1947
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 could not find anything that explains the need of this "operator+".
43
4816
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: // ... };
1
9103
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5967
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3175
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2539
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2118
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.