9

I'm trying to multiply two matrices stored inside 1d arrays.

I'm using this function, but my program crashes, I assume due to an out of bounds error. However, I have no (easy) ability to debug, so I have to decide if my code is correct, and to me it seems it is...

void SampleUtils::multiplyMatrices(float* matA, int rA, int cA, float* matB,
        int rB, int cB, float* matC, int rC, int cC) {
    for (int i = 0; i <= rA; i++) {
        for (int j = 0; j <= cB; j++) {
            float sum = 0.0;
            for (int k = 0; k <= rB; k++)
                sum = sum + matA[i * cA + k] * matB[k * cB + j];
            matC[i * cC + j] = sum;
        }

    }

So, can anyone find out what I did wrong?

Thanks...

5
  • 1
    If rA is the number of rows of the matrix, then the condition must be i < rA. Similarly at other places. Commented Apr 20, 2012 at 19:50
  • 1
    1. Use a debugger to find out where it crashes. 2. Use vectors and call at to get an exception thrown if it is an out-of-bounds error. Commented Apr 20, 2012 at 19:50
  • 1
    "... to me it seems it is...." - the fact that your program crashes tells you that this statement is quite incorrect. You'll find a solution faster if you start with the assumption that your code is wrong, and you're the one that made it so. Commented Apr 20, 2012 at 19:50
  • 1
    Isn't rB always equal to cA, and rC is not used? Generally, I think you need only three sizes (ra=rC, rb=cA, and cb=cC, if I remember it correctly), not six; consider eliminating the unused parameters to reduce the confusion. Commented Apr 20, 2012 at 19:53
  • 1
    @duffymo - yeah, I think it is incorrect. perhaps my English is off, but it was my way of saying: "I can't figure out what's the hell wrong with it" :-) Also, sum should be a float obviously :-P Commented Apr 20, 2012 at 19:54

2 Answers 2

12

Chances are you mean < instead of <= in your for loops.

Sign up to request clarification or add additional context in comments.

Comments

7

Try to use i < rA , j < cB, k < rB in your for

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.