CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Bugs

Problem with copy Assignment operator of Matrix

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   October 16, 2007, 00:16
Default Dear everyone I have encou
  #1
Senior Member
 
su_junwei's Avatar
 
su junwei
Join Date: Mar 2009
Location: Xi'an China
Posts: 151
Rep Power: 20
su_junwei is on a distinguished road
Send a message via MSN to su_junwei
Dear everyone

I have encountered a segmentation fault with the class of Matrix<t>. Is it a bug, or any other reason?


file: src/OpenFOAM/containers/Matrix/Matrix.C

template<class>
void Matrix<t>::operator=(const Matrix<t>& a)
{
if (this == &a)
{
FatalErrorIn("Matrix<t>::operator=(const Matrix<t>&)")
<< "attempted assignment to self"
<< abort(FatalError);
}

T* v = v_[0]; // before deference, need to allocate memory for v_ and deallocate the memory for v_ allocated avoiding memory leak.
const T* av = a.v_[0];

label nm = n_*m_;
for (register label i=0; i<nm; i++)
{
v[i] = av[i];
}
}



test:
Matrix<scalar> a(2,2,3);
Matrix<scalar> b(2,2,1);

Matrix<scalar> c=b; //OK copy constructor

Matrix<scalar> d;
d=a+b; // error, segmentation fault! the memory of d is not allocated.

Info<<a+b<<endl; //OK copy constructor

Is it true ???

Junwei
su_junwei is offline   Reply With Quote

Old   October 16, 2007, 02:56
Default The problem is yours I'd say.
  #2
Super Moderator
 
niklas's Avatar
 
Niklas Nordin
Join Date: Mar 2009
Location: Stockholm, Sweden
Posts: 693
Rep Power: 29
niklas will become famous soon enoughniklas will become famous soon enough
The problem is yours I'd say.
Matrix<scalar> d;

where is the size set in this?

Im a bit surprised that it's allowed to have a null constructor actually.

It's like trying to add vectors a and b
vector a = (1)
vector b= (1,2)
niklas is offline   Reply With Quote

Old   October 16, 2007, 04:11
Default Currently the assignment opera
  #3
Senior Member
 
Join Date: Mar 2009
Posts: 854
Rep Power: 22
henry is on a distinguished road
Currently the assignment operator assumes that the LHS is the same size as the RHS and copies the elements which clearly in your case does not work. I think that the code should check that the sizes of the LHS and RHS are the same. For your code to work you would need to pre-allocate the matrix d with the appropriate sizes.

Given that null-construction is allowed perhaps it would be better if the LHS is cleared and resized automatically to match the RHS (as is the case to List for example) in which case your code would then work as-is. I will make this change and post the code.
henry is offline   Reply With Quote

Old   October 16, 2007, 04:27
Default Hi Niklas and Henry actu
  #4
Senior Member
 
su_junwei's Avatar
 
su junwei
Join Date: Mar 2009
Location: Xi'an China
Posts: 151
Rep Power: 20
su_junwei is on a distinguished road
Send a message via MSN to su_junwei
Hi Niklas and Henry

actually, the size of the RHS can be assigned in the definition of the function as

template<class>
void Matrix<t>::operator=(const Matrix<t>& a)
{

if (this == &a)
{
FatalErrorIn("Matrix<t>::operator=(const Matrix<t>&)")
<< "attempted assignment to self"
<< abort(FatalError);
}
if(a.empty()) //check if a is empty(),
{
FatalErrorIn("Matrix<t>::operator=(const TMatrix<t>& a)")
<< "a is empty: a.n_= "<<a.n_<<" a.m_="<<a.m_<<nl
<< abort(FatalError);
}

if(a.n_!=n_||a.m_!=m_)
{
deallocate(); //a function deallocate the memory allocated for LHS
n_=a.n_;
m_=a.m_;
allocate(); //reallocate
}

T* v = v_[0];
const T* av = a.v_[0];
label nm = n_*m_;
for (register label i=0; i<nm; i++)
{
v[i] = av[i];
}
}

the code is similar to the definition of assignment operator of List at

/src/OpenFOAM/containers/Lists/List/List.C

Thus,It seems that all work done.

yours

Junwei
su_junwei is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Matrix solving problem thermoconvection kar OpenFOAM Running, Solving & CFD 0 May 3, 2008 14:44
Addressing matrix element and reuse of system matrix marziolettich OpenFOAM Running, Solving & CFD 2 February 19, 2008 06:04
Simple problem of "copy" command in Gambit. jason FLUENT 0 December 4, 2007 02:59
problem about direct solver for sparse matrix ztdep Main CFD Forum 0 August 11, 2006 13:16
UDF memory assignment Kate FLUENT 0 February 2, 2006 09:15


All times are GMT -4. The time now is 21:46.