im currently writing a class which stores matrices and performs arithmic operations with them.
the matrices are stored as follows:
Code:
float *pfaMatrix = new float[nColumnCount + nRowCount];
to access the matrix easily, the following is used:
Code:
index = nRow * nColumnCount + nCol
using those methods, i can use the array as if it was a two-dimensional array but without the restriction that i have to do this
Code:
float *pfaMatrix = new float[nRowCount][CONST];
my problem is now, that i do not really know, how to write the function of the overloaded * operator to multiply two matrices. maybe i just need the right approach. in the beginning of the funct. i already made a new matrix of the correct size:
Code:
float *pfaResMatrix = new float[cMatrix1.m_nRowCount + cMatrix2.m_nColumnCount];
n=a*y+b*z and m=c*y+d*z
i figured that it has to be somewhat like this:
for(int nRo = 0; nRow < resmatrix.m_nRowCount; nRow++)
{
for(int nCol = 0; nCol < resmatrix.m_nColumnCount; nCol++)
{
now i have acces to the matrix element holding the resolution(n)
resmatrix.pfaMatrix[nRow * resmatrix.m_nColumnCount + nCol] = /*What should the equation be here*/
}
}
sorry for the screwed up double post.. no edit button.
Last edited by replax; March 28th, 2010 at 06:04 PM.
it means something like multiply the elements of the ith row of A with the elements of the jth column of B to get the i-jth element of M (where M = AB).
2) Why doesn't your Matrix constructor generate the memory?
the problem is, if I use my Matrix constructor to construct a matrix, it will call the destructor before returning the matrix value. thus crashing the programm.
i solved the problem, i did an unbelievably stupid error. in the main.cpp file where i did execute the arithmic expressions i typed '+' instead of '*', which of course gives me my matrix addition instead of multiplication.
the problem is, if I use my Matrix constructor to construct a matrix, it will call the destructor before returning the matrix value. thus crashing the programm.
That does not really make sense. The destructor will not normally be called from the constructor.
On a hunch: did you implement the copy constructor and copy assignment operator?
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
no, I did not implent the copy constructor.
So when doing
Code:
return cResMatrix;
return does copy the matrix and then returns the copy because the original matrix goes out of scope, right? and when the matrix goes out of scope (after the curly braces) it calles the destructor, correct?
so I would need to write a copy constructor, so that return knows how to copy a matrix? buy why do I not need to do that when I use:
Code:
return Matrix(nRowCount, nColCount, pfaResMat);
because the returned type is a matrix, too.
Or did I get something essential completly wrong?
regards,
replax
Last edited by replax; March 29th, 2010 at 04:27 AM.
no, I did not implent the copy constructor.
So when doing
Code:
return cResMatrix;
return does copy the matrix and then returns the copy because the original matrix goes out of scope, right? and when the matrix goes out of scope (after the curly braces) it calles the destructor, correct?
so I would need to write a copy constructor, so that return knows how to copy a matrix? buy why do I not need to do that when I use:
Code:
return Matrix(nRowCount, nColCount, pfaResMat);
because the returned type is a matrix, too.
Or did I get something essential completly wrong?
regards,
replax
The principle of the matter is robust code, that means that you should minimise the number of places that you allocate and deallocate memory in your class - when you get on to the subject of exception saftey you will appreciate this point more. A proven pattern for a safe/robust way of allocation and deallocation of resources is the RAII (Resource Acquisition Is Initialization) idiom. This would be a good thing for you to look up, as well as the Rule of Three - i.e. if you write one of the following for your class, then you should probably write all three:
The point in hand, is that you could save yourself a lot of bother and bugs if you adhere to the advice that you are being given with respect to memory management.
std::vector will handle the allocation and deallocation for you - you also do not need to write a destructor, a copy assignment operator or a copy constructor, since the default generated ones will be perfectly fine.
Note that the operator()(size_t rows, size_t columns) allows you to access the relevent matrix element.
Code:
int main()
{
matrix m(3,3); //create a 3x3 matrix
m(1,1) = 5.2f; //set the central element to 5.2
}
the problem is, if I use my Matrix constructor to construct a matrix, it will call the destructor before returning the matrix value. thus crashing the programm.
That is because you didn't write an appropriate copy constructor and assignment operator. How many C++ classes have you used, where your code needs to allocate the memory for the class? I bet none, except only yours.
For example std::string -- why is it that we don't need to allocate memory for the string data? What is the magic that std::string does that your Matrix class is not doing? The answer is that the std::string has implemented proper copy semantics, and your Matrix class hasn't.
Code:
#include <string>
std::string DoStuff()
{
std::string s = "abc123";
s += "456";
s = s.substr(0,1);
return s;
}
There is no crash, because on return, the std::string copy constructor makes a copy of s and returns it. Without a user-defined copy constructor, the std::string would suffer the same problem that your Matrix class suffers from.
To correct the problem, I could start naively allocating memory all over the place to make sure the crash doesn't happen, or I give std::string some intelligence and write functions (copy/assignment operator) that tells std::string what to do when making copies of itself.
When you create an object, that object is supposed to handle its own memory, and clean up its own memory. The user of the object shouldn't be doing this work -- it the user has to do this work, then that class is basically useless in a program. The user has to jump through hoops keeping the object valid, doing work the class should be doing. That defeats the whole purpose of using the class to begin with.
i solved the problem, i did an unbelievably stupid error. in the main.cpp file where i did execute the arithmic expressions i typed '+' instead of '*', which of course gives me my matrix addition instead of multiplication.
That doesn't mention the problem I pointed out to you, and that is you are allocating memory that is not initialized to 0. Did you fix that problem?
as for now, I got it working as far as I can see it. Now my constructors are the same, I just fixed the allocation that was not initialized to 0 and added the copy constructor:
Bookmarks