Dear ROOTers
I know this is not the typical ROOT question, but I have created following function where I use TMath::Sort():
Double_t TStat::Median(Int_t n, const Double_t *arr)
{
if (n <= 0) return NA_REAL;
if (n == 1) return arr[0];
// Create index and sort array
Int_t *index = 0;
if (!(index = new (nothrow) Int_t[n])) {
cout << "Error: Could not initialize memory!" << endl;
return NA_REAL;
}//if
TMath::Sort(n, arr, index);
// Find median
Int_t k;
Double_t median = 0.0;
if ((n % 2) == 0){
k = (Int_t)floor(n / 2.0) - 1;
median = (arr[index[k]] + arr[index[k+1]])/2.0;
} else {
k = (Int_t)floor(n / 2.0);
median = arr[index[k]];
}//if
delete [] index;
return median;
}//Median
This function seems to work fine since many years, but now a user of my program sent me the stacktrace of R
executed under gdb. She needed to set the MALLOC_CHECK_ environment variable to 1 otherwise the program hangs.
#0 0x00c6ad3b in malloc_check () from /lib/libc.so.6
#1 0x00c6a6b5 in malloc () from /lib/libc.so.6
#2 0x06007b47 in operator new () from /usr/lib/libstdc++.so.6
#3 0x06007c24 in operator new[] () from /usr/lib/libstdc++.so.6
#4 0x00fef967 in TStat::Median () from
/home/carissimo/local/lib/R/library/xps/libs/xps.so
#5 0x00fefb21 in TStat::TukeyBiweight () from
/home/carissimo/local/lib/R/library/xps/libs/xps.so
Dou you have any idea what the reason for this stacktrace could be?
Is there a problem with my TStat::Median() code?
Do I have to set “index” to Long64_t?
Thank you in advance
Best regards
Christian