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);
}
6
  • maybe he doesnt support floating point?
    – Zelldon
    Apr 27, 2015 at 8:26
  • 1
    Best to isolate the floating point issue like @Zelldon suggests. What happens if you malloc(20) into an int* ? Apr 27, 2015 at 8: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, 2015 at 8:27
  • @trilolil If you want array of doubles, then sizeof(double*) is definitely wrong. Perhaps you need to post more code.
    – user694733
    Apr 27, 2015 at 9: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 doubles. 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, 2015 at 9:03

3 Answers 3

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.

4
  • This is actually the only malloc I use. I didn't do anything for the heap. Hereby a screenshot: imgur.com/JT5W5tK Apr 27, 2015 at 12:10
  • Well all malocs anywhere fail. I cannot assign any value to a "malloced" array. Apr 27, 2015 at 12:18
  • 1
    You need to check your compiler/linker doc to correctly initialize your libc and/or you heap. If you have enough RAM and no threads, a much more simple solution is to allocate a global variable for the biggest possible allocation and use it.
    – calandoa
    Apr 27, 2015 at 12:52
  • You were right! thank you very much. There was no heap allocated at all. 0 Apr 27, 2015 at 14:13
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.

1
  • 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) Apr 27, 2015 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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.