473,395 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

problem with constructor/destructor?

Probably i have problem with co/des :

if i call functions wich returns farray, programs go seg fault.

if i comment destructor.. programs work but do not free memory.

Any suggestion?

farray and fmatrix are fast 1-index based array and matrix. f means fortran.

my aim is to write a very simple class faster than TNT (template
numerical toolkit)

// -*-C++-*-
#include <cmath>
using std::sqrt;
namespace tonymatrix
{
template <class T>
class farray
{
T* v_;
int dim_;
public:
explicit inline farray(): dim_(0), v_(0) {};
explicit inline farray (int a): dim_(a) {v_= new T[dim_];}
// inline ~farray() {delete []v_;};
inline farray (const farray &b):dim_(b.size()) { v_= new T[dim_];
for (int i=0;i!=dim_;i++) v_[i]=b.v_[i]; }
inline T& operator() (int i){return v_[i-1];}
inline const T& operator() (int i) const {return v_[i-1];}
inline T& operator[] (int i){return v_[i-1];}
inline const T& operator[] (int i) const {return v_[i-1];}
inline int dim() const {return dim_;}
inline int dim1() const {return dim_;}
inline int size() const{return dim_;}
inline T sum() const {T res; for (int i=0;i!=dim_;i++) res+=v_[i];
return res;}
};
template <class T>
class fmatrix {
T* v_;
int dim1_,dim2_;
public:
explicit fmatrix (int a,int b): dim1_(a), dim2_(b) {v_= new T[a*b]; }
explicit inline fmatrix (const fmatrix &b):
dim1_(b.dim_1),dim2_(b.dim2_) {int dim=dim1_*dim2_;v_= new T[dim]; for
(int i=0;i!=dim;i++) v_[i]=b.v_[i];}
~fmatrix() {delete []v_;}
inline T& operator() (int i, int j){return v_[(i-1)*dim1_+j-1];}
inline const T& operator() (int i, int j) const {return
v_[(i-1)*dim1_+j-1];}
inline int dim1() const {return dim1_;}
inline int dim2() const {return dim2_;}
inline int size() const {return dim1_*dim2_;}
inline T sum() const {T res; int dim=dim1_*dim2_; for (int
i=0;i!=dim;i++) res+=v_[i]; return res;}
};
template <class T> inline T dot_product(const farray<T> &vec1, const
farray<T> &vec2)
{
T res=0;
int a=vec1.size(), b=vec2.size();
if (a>b) a=b;
for (int i=1;i<=a;i++)
{
res+=vec1[i]*vec2[i];
}

return res;
}
template <class T> inline double norm(const farray<T> &vec1)
{
return sqrt((dot_product(vec1,vec1)));
}
template <class T> inline void normalize (farray<T> &vec)
{
int a=vec.size();
double denom= norm(vec);
if (denom!=0) for (int i=1;i<=a;i++) vec[i]/=denom;
}
}

--
Hai recensioni nel cassetto di musica, cinema, letteratura,fumetti??
Ascolta e pentiti e se hai tempo vergognati
http://tonysuper.altervista.org
Jul 22 '05 #1
9 1658

"tonysuper" <mi*****@altervista.org> wrote in message
news:2m************@uni-berlin.de...
Probably i have problem with co/des :

if i call functions wich returns farray, programs go seg fault.

if i comment destructor.. programs work but do not free memory.

Any suggestion?


without looking at your code, you should think to yourself at this point:

My program is crashing when trying to free memory, therefore I have either:
1) already freed the memory
2) have written/accessed my memory past the allocated bounds.

Unless you have other code in your d'tor which is causing the problems.
Allan
Jul 22 '05 #2
tonysuper wrote:
Probably i have problem with co/des :

if i call functions wich returns farray, programs go seg fault.

if i comment destructor.. programs work but do not free memory.

Any suggestion?

farray and fmatrix are fast 1-index based array and matrix. f means
fortran.


Possibly this may indirectly be the cause of the problem. There is no
index checking in your code. So if someone accidentally calls uses a 0
index, the memory that the destructor needs for proper cleanup might get
corrupted. This may go undetected as long as the destructor isn't
called. What you could do is place assert(i>0) at strategic places (such
as operator[]) to detect when someone uses an invalid index.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 22 '05 #3
Peter van Merkerk ha scritto:
tonysuper wrote:
Probably i have problem with co/des :

if i call functions wich returns farray, programs go seg fault.

if i comment destructor.. programs work but do not free memory.

Any suggestion?

farray and fmatrix are fast 1-index based array and matrix. f means
fortran.

Possibly this may indirectly be the cause of the problem. There is no
index checking in your code. So if someone accidentally calls uses a 0
index, the memory that the destructor needs for proper cleanup might get
corrupted. This may go undetected as long as the destructor isn't
called. What you could do is place assert(i>0) at strategic places (such
as operator[]) to detect when someone uses an invalid index.


even with bound checking the problem remains... it is not a problem of bound

--
Hai recensioni nel cassetto di musica, cinema, letteratura,fumetti??
Ascolta e pentiti e se hai tempo vergognati
http://tonysuper.altervista.org
Jul 22 '05 #4

"tonysuper" <mi*****@altervista.org> wrote in message
news:2m************@uni-berlin.de...
Probably i have problem with co/des :

if i call functions wich returns farray, programs go seg fault.

if i comment destructor.. programs work but do not free memory.

Any suggestion?

farray and fmatrix are fast 1-index based array and matrix. f means fortran.
my aim is to write a very simple class faster than TNT (template
numerical toolkit)

// -*-C++-*-
#include <cmath>
using std::sqrt;
namespace tonymatrix
{
template <class T>
class farray
{
T* v_;
int dim_;
public:
explicit inline farray(): dim_(0), v_(0) {};
explicit inline farray (int a): dim_(a) {v_= new T[dim_];}
// inline ~farray() {delete []v_;};
inline farray (const farray &b):dim_(b.size()) { v_= new T[dim_];
for (int i=0;i!=dim_;i++) v_[i]=b.v_[i]; }
inline T& operator() (int i){return v_[i-1];}
inline const T& operator() (int i) const {return v_[i-1];}
inline T& operator[] (int i){return v_[i-1];}
inline const T& operator[] (int i) const {return v_[i-1];}
inline int dim() const {return dim_;}
inline int dim1() const {return dim_;}
inline int size() const{return dim_;}
inline T sum() const {T res; for (int i=0;i!=dim_;i++) res+=v_[i];
return res;}
};


You have not defined an assignment operator for your class, therefore you
are freeing the same memory twice.

john
Jul 22 '05 #5
ok

it was the operator= missing
now it works
any suggestion for efficiency in this class?

template <class T>
class farray
{
T* v_;
int dim_;
public:
explicit inline farray(): dim_(0), v_(0) {};
explicit inline farray (int a): dim_(a) {v_= new T[dim_];}
inline ~farray() {delete [] v_; v_= 0; };
inline farray (const farray &b):dim_(b.size()) { v_= new T[dim_];
for (int i=0;i!=dim_;i++) v_[i]=b.v_[i]; }
inline farray operator=(const farray&b)
{
if (v_!=0) {
delete [] v_;
v_=0;
}
dim_=b.dim_;
v_=new T[dim_];
for (int i=0;i!=dim_;i++) v_[i]=b.v_[i];
return *this;
}

inline T& operator() (int i){assert(i<=dim_&&i>0); return v_[i-1];}
inline const T& operator() (int i) const
{assert(i<=dim_&&i>0);return v_[i-1];}
inline T& operator[] (int i){assert(i<=dim_&&i>0);return v_[i-1];}
inline const T& operator[] (int i) const
{assert(i<=dim_&&i>0);return v_[i-1];}
inline int dim() const {return dim_;}
inline int dim1() const {return dim_;}
inline int size() const{return dim_;}
inline T sum() const {T res; for (int i=0;i!=dim_;i++) res+=v_[i];
return res;}
};


--
Hai recensioni nel cassetto di musica, cinema, letteratura,fumetti??
Ascolta e pentiti e se hai tempo vergognati
http://tonysuper.altervista.org
Jul 22 '05 #6

"tonysuper" <mi*****@altervista.org> wrote in message
news:2m************@uni-berlin.de...
ok

it was the operator= missing
now it works
any suggestion for efficiency in this class?

template <class T>
class farray
{
T* v_;
int dim_;
public:
explicit inline farray(): dim_(0), v_(0) {};
explicit inline farray (int a): dim_(a) {v_= new T[dim_];}
inline ~farray() {delete [] v_; v_= 0; };
inline farray (const farray &b):dim_(b.size()) { v_= new T[dim_];
for (int i=0;i!=dim_;i++) v_[i]=b.v_[i]; }
inline farray operator=(const farray&b)
{
if (v_!=0) {
delete [] v_;
v_=0;
}
dim_=b.dim_;
v_=new T[dim_];
for (int i=0;i!=dim_;i++) v_[i]=b.v_[i];
return *this;
}
This will fail on self assignment

farray x(10);
x = x; // this crashes

Of course you would never code that but its quite easy to code something
that does result in self assignment so you should protect yourself against
it.

inline T& operator() (int i){assert(i<=dim_&&i>0); return v_[i-1];}
inline const T& operator() (int i) const
{assert(i<=dim_&&i>0);return v_[i-1];}
inline T& operator[] (int i){assert(i<=dim_&&i>0);return v_[i-1];}
inline const T& operator[] (int i) const
{assert(i<=dim_&&i>0);return v_[i-1];}
inline int dim() const {return dim_;}
inline int dim1() const {return dim_;}
inline int size() const{return dim_;}
inline T sum() const {T res; for (int i=0;i!=dim_;i++) res+=v_[i];
return res;}
};


Use reference counting with copy-on-write. You should avoid copying as much
as possible.

john
Jul 22 '05 #7
This will fail on self assignment

farray x(10);
x = x; // this crashes
if (this= &b) return *this;
at the first line is sufficient?

Use reference counting with copy-on-write. You should avoid copying as much
as possible.


what do you mean?
can you make me an example of inefficiency with my code?

--
Hai recensioni nel cassetto di musica, cinema, letteratura,fumetti??
Ascolta e pentiti e se hai tempo vergognati
http://tonysuper.altervista.org
Jul 22 '05 #8

"tonysuper" <mi*****@altervista.org> wrote in message
news:2m************@uni-berlin.de...
This will fail on self assignment

farray x(10);
x = x; // this crashes
if (this= &b) return *this;
at the first line is sufficient?


Yes.

Use reference counting with copy-on-write. You should avoid copying as much as possible.
what do you mean?


For a quick introduction to reference counting and copy on write try these
articles http://www.gotw.ca/gotw/043.htm, http://www.gotw.ca/gotw/044.htm,
http://www.gotw.ca/gotw/045.htm
can you make me an example of inefficiency with my code?


farray some_calculation()
{
farray res;
...
return res;
}

farray a = some_calculation();

It's likely that when some_calculation returns the entire farray will be
copied. With reference counting you can be sure that it will not be.

john
Jul 22 '05 #9
It's likely that when some_calculation returns the entire farray will be
copied. With reference counting you can be sure that it will not be.


yeah i understand...
but if i'd need this stuff probably i'll better use directly the very
good TNT by Pozo and adapt to my need adding some stuff.

In fact my idea was just to write a faster and simpler version of Tnt...
it is simpler in the mean that the assembly code of my program does not
contain any call like the code generated by the fortran litteral
translation.

TNT which is ref counted instead generates a function to access array
elements .

Strangely performance difference between my class and TNT are less than
0.5 % (probably cause RAM is slower than cache-function call) and both
with gnu g++ are 40 % FASTER than F90 compiled with Intel ifort 8.0

both g++ and ifort are used with a lot of flags to assure best
optimization.

--
Hai recensioni nel cassetto di musica, cinema, letteratura,fumetti??
Ascolta e pentiti e se hai tempo vergognati
http://tonysuper.altervista.org
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Robin Forster | last post by:
I have two classes: aule_gl_window (parent class) and aule_button (sub class) I want to call the super class (parent) constructor code from the sub class constructor.
7
by: Emanuel Ziegler | last post by:
Hello, I want to do some mathematics with functions. In my case the function classes are very complex, but this simple example has the same problems. To allow calculations that begin with a...
1
by: Spur | last post by:
Hi all, I implemented a memory allocation/deallocation class that logs all new/delete calls (overloaded) and remembers for each allocated block where it was allocated from (using a macro that...
18
by: bArT | last post by:
Hi! I have a problem with such situation. I have a class like below and have some pointers (ptr1 and ptr2). I dynamically allocate memory in constructor and I free in destructor. For one pointer...
5
by: August1 | last post by:
This is a short program that I have written from a text that demonstrates a class object variable created on the stack memory and another class object variable created on the heap memory. By way...
3
by: rahul8143 | last post by:
hello, I write a following program and have problem in understanding constructors and destructors. #include <iostream.h> class vector { public: double x; double y;
14
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
7
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
3
by: GAURAV AGRAWAL | last post by:
Hi Guys, Can someone please explain me why this is happening #include<iostream> using namespace std; class a { public: int a1; // If I remove this it'll work fine
6
by: asm23 | last post by:
Hi, everyone, I'm learning <<thinking c++>volume Two, and testing the code below. ///////////////////////////////////////////////////////////////////////////// //: C01:Wrapped.cpp #include...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...

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.