Tool/software: TI C/C++ Compiler
Hello everyone
I am using the ARM core on the AWR1642, the compiler version is v16.9.1.LTS. I want to sort an array structure as showing below, however, when I run the code, it gives the error. It seems like the qsort want to access to an invalid area. Can you tell me what is the wrong?
Thanks
Xining
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct {
float price;
float test;
int id;
} order;
order list[10];
int i = 0;
int compare (const void * a, const void * b)
{
order *orderA = (order *)a;
order *orderB = (order *)b;
return ( (orderA->price > orderB->price) ? 1:-1 );
}
int main ()
{
srand((unsigned int)time(NULL));
printf("Before sorting\n");
for(i=0; i<10; i++){
list[i].price = (float)rand()/(float)(RAND_MAX) * 10;
list[i].test = rand()%10;
list[i].id = i;
printf ("Order id = %d Price = %f Test = %f\n",list[i].id, list[i].price, list[i].test);
}
printf("AFTER sorting\n");
int n;
qsort (list, 6, sizeof(order), compare);
for (n=0; n<10; n++)
printf ("Order id = %d Price = %f Test = %f\n",list[n].id, list[n].price, list[n].test);
return 0;
}
The output is:
Before sorting
Order id = 0 Price = 6.347239 Test = 8.000000
Order id = 1 Price = 1.647694 Test = 3.000000
Order id = 2 Price = 5.049898 Test = 5.000000
Order id = 3 Price = 8.674276 Test = 4.000000
Order id = 4 Price = 4.470962 Test = 0.000000
Order id = 5 Price = 6.625263 Test = 6.000000
Order id = 6 Price = 3.482467 Test = 9.000000
Order id = 7 Price = 6.930449 Test = 9.000000
Order id = 8 Price = 5.341044 Test = 6.000000
Order id = 9 Price = 5.465560 Test = 2.000000
AFTER sorting
Exception occurred in ThreadType_Main.
Main handle: 0x0.
Main stack base: 0x8019a40.
Main stack size: 0x1000.
R0 = 0x08040000 R8 = 0x080199fc
R1 = 0x080199e4 R9 = 0x08040000
R2 = 0x0000000c R10 = 0x00008710
R3 = 0x00008710 R11 = 0x00000003
R4 = 0x00000005 R12 = 0x08040000
R5 = 0x00003330 SP(R13) = 0x0801a9e8
R6 = 0x0000000c LR(R14) = 0x000068c3
R7 = 0x00000006 PC(R15) = 0x00008738
PSR = 0x000c01df
DFSR = 0x0000000d IFSR = 0x00000000
DFAR = 0x08040000 IFAR = 0x00000000
ti.sysbios.family.arm.exc.Exception: line 205: E_dataAbort: pc = 0x00008738, lr = 0x000068c3.
xdc.runtime.Error.raise: terminating execution