8

I am writing a function on a 32-bit cortex m4 microcontroller. The function has to be able to multiply matrices of different sizes, which I cannot predict. So i Have to use a malloc...

But I don't understand why my mc always goes into default handler interrupt when it executes following line:

double *output2=NULL;
output2 = malloc(3 *1* sizeof(double *));

Is this mc not able to handle this type of operations? While this works perfectly fine on my laptop!

**EDIT*

Here by some more code (which still needs to be modified...): Well all malocs anywhere fail. I cannot assign any value to a "malloced" array.

int main (void)
{

    /*some stuff*/

    float transFRotMatrix[3][3]={0}; //array gets modified by other functions
    float sunMeasurements[3][1] = {{1},{2},{3}}; //test values

        multiplyMatrices( &transFRotMatrix[0][0],3, 3, &sunMeasurements[0][0], 3, 1, *orbitalSunVector);

    /*some stuff*/
}

void multiplyMatrices(float *transposedMatrix, int height1, int width1, float *iSunVector,int height2, int width2, float *orbitalSunVector)
{

    int y=0;
    int x = 0;
    int row=0;
    int column =0;
    int k=0;
    int k2 = 0;
    float result = 0;

    int i=0;
    int j=0;
    int t=0;

    float rotationMatrix[3][3]={0};

    i=0;
    k=0;
    k2 = 0;


    if(width1 != height2)
    {
        printf("unmatching matrices, error.\n\n");
        return;
    }

    float *output2;

    output2 = malloc(3 *1* sizeof(float *)); //<-----ERROR


    while(k<width1) //aantal rijen 1ste matrix
    {
        for(j=0;j<height2;j++) //aantal rijen 2de matrix
        {
            result += (*((transposedMatrix+k*width1)+j)) * (*((iSunVector+j*width2)+k2));  //1ste var:aantal kolommen 2de matrix  --2de variabele na de plus = aantal kolommen 2de matrix
            //printf("%f * %f\t + ", (*((transposedMatrix+k*width1)+j)), (*((iSunVector+j*width2)+k2)));
        }

        output2[k*3 +k2] = result;  //<-----FAILS HERE


        k2++;
        x++;
        column++;

        if(x==width2)
        {
            k2=0;
            x=0;
            column=0;
            row++;
            y++;
            k++;

        }
        result = 0;

    }

    for(i=0;i<height1;i++)
    {
        for(j=0;j<width2;j++)
        {
             orbitalSunVector[j * height1 + i] = output2[i*3 +j];
        }
    }
    free(output2);
}
  • maybe he doesnt support floating point? – Zelldon Apr 27 '15 at 08:26
  • 1
    Best to isolate the floating point issue like @Zelldon suggests. What happens if you malloc(20) into an int* ? – Ishay Peled Apr 27 '15 at 08:27
  • 2
    Are you 100% sure it is the malloc call which fails, and not some code after it? Afaik you should be allocating `3 * sizeof( double )` (at least if I understand it correctly - your code is very hard to read due to the way you use spaces) - that is likely twice the amount of bytes as `3 * sizeof( double* )` so you might be accessing out of bounds data somewhere – stijn Apr 27 '15 at 08:27
  • @trilolil If you want array of doubles, then `sizeof(double*)` is definitely wrong. Perhaps you need to post more code. – user694733 Apr 27 '15 at 09:01
  • 2
    As pointed out by others, `output2 = malloc(3 *1* sizeof(double *));` is wrong, it should be `output2 = malloc(3 * 1 * sizeof *output2);` to allocate 3x1 `double`s. By dereferencing the `output2` pointer we don't need to repeat the type name, and will get an error if `output2` is renamed or removed. This fix doesn't address the fault you're seeing, though. – unwind Apr 27 '15 at 09:03
  • Best practices recommend to check the [`malloc()`](http://en.cppreference.com/w/c/memory/malloc) return value. You don't do that. If indeed it fails, then at least you could handle it properly. – kebs Apr 27 '15 at 11:00

3 Answers3

0

There are two valid types of implementations in C: hosted and freestanding. Programs written for hosted implementations may use any of the standard library headers. However, freestanding implementations are only required to support <float.h>, <iso646.h>, <limits.h>, <stdalign.h>, <stdarg.h>, <stdbool.h>, <stddef.h>, <stdint.h>, and <stdnoreturn.h>.

It's not uncommon for microcontrollers to have minimal support, so perhaps your C compiler (or rather, standard library) doesn't have full support for <stdlib.h> (and hence malloc). However, without knowing which compiler and standard library you've chosen to use, that's just a guess.

It's also possible that, as the comments for your question suggest, something prior to (and/or following) this piece of code is to blame. Perhaps you could produce an mcve?

Please feel free to comment underneath this answer when you've updated to give more information, and I'll happily elaborate to provide more information.

Community
  • 1
  • 1
autistic
  • 1
  • 3
  • 35
  • 80
  • Thank you for your answer. I am coding using code composer studio from texas instrument 5.4.0.00091. I edited my question to provide some more code. (altough there is strictly nothing more previously to those lines) – LandonZeKepitelOfGreytBritn Apr 27 '15 at 10:43
0

What you received was an processor exception, not an interrupt. They're all mapped to the same handler which is a common practice. I assume that you got Hardware fault. You can check the registers to see what caused the exception. Use this code as a reference to examine them. It could be related to the libc implementation if the exception occurs inside malloc.

stdcall
  • 27,613
  • 18
  • 81
  • 125
0

Is the malloc() working fine in other part of this code ? As you are programming an embedded device, the heap may not have been properly initialized, or it have been initialized for another custom malloc()-like function.

calandoa
  • 5,668
  • 2
  • 28
  • 25