Getting ET_LOAD_STORE, Memory access exception. Topic is solved

If you have a simple question and just want an answer.
mogren3000
Member++
Posts: 19
Joined: Tue Apr 29, 2014 8:12 am

Getting ET_LOAD_STORE, Memory access exception.

Post by mogren3000 »

Hi everybody. I am new and trying to run a recursive factorial code on the startKIT and after a while I am getting: "Program received signal ET_LOAD_STORE, Memory access exception.".
What i think is that the MCU only seems to support 64bit and cant hold the numbers for factorial 50.
But I am not sure.
Here is my code.
int factorial(int n)
{
if(n!=1)
return n*factorial(n-1);
return 1;
}
int main()
{
uint64_t i;
int VALUE[37]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,25,50,70,100,450,1000,3249,10000,25206,100000,205023,1000000,1723508,2000000,10000000,14842907};
for (i=1; i<=VALUE; i++)
{
printf("Factorial of %d is %lu\n",VALUE,factorial(VALUE));
}
return 0;
}



View Solution
User avatar
Bianco
XCore Expert
Posts: 754
Joined: Thu Dec 10, 2009 6:56 pm

Post by Bianco »

Hi,You access the array outside the bound.Using the condition <= 36 works fine.XC has run-time checks on out of bound access of arrays by default.An out of bound access will trap an exception.You can disable this using #pragma unsafe arrays.C does not have run-time checks on arrays so if you save the code into a C file it will not cause an exception either.20! is the largest that fits in a 64-bit number by the way.
richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

I suspect the reason for the load / store exception is a stack overflow. Since factorial() is recursive it will use at least one one word (4 bytes) of memory each time it calls it self. For large n the stack will overflow the amount of memory available and start overwriting the program, at which point anything could happen (including a load / store exception). Using a non recusive version of factorial should fix this:

int factorial(int n)
{
 int result = 1;
 while (n > 1) {
   result *= n;
   --n;
 }
 return result;
}

If you fix the stack overflow you will get an array bound exception (ET_ECALL). This is because the exit condition of the following for loop will never be met:

for (i=1; i<=VALUE; i++) {
 ...
}

As such i will continue to be incremented until it equals the size of the VALUE array (37) at which point the expression VALUE[37] will be out of bounds and a runtime exception will be raised. You can fix this by changing the exit condition of the loop, for example by changing it to:

for (i=1; i<37; i++) {
 ...
}

Once you've made this change your program should run to completion, however for large values of n you still won't get the right result. The factorial function takes a int argument which on the xCORE is 32 bits wide. If n! doesn't fit in a 32bit value then the multiplication inside the factorial function will overflow. This won't cause an exception but the result of factorial() won't match the mathematical value of factorial due to this overflow.