473,695 Members | 1,785 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 2275

"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
3660
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
4324
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
1789
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
2568
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
2398
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
1946
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
4813
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
8562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9113
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8976
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8830
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
8820
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...
0
7655
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4339
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
4575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2265
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.