Malloc error in my function TStat::Median()

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;
   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]];

   delete [] index;

   return 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/
#1  0x00c6a6b5 in malloc () from /lib/
#2  0x06007b47 in operator new () from /usr/lib/
#3  0x06007c24 in operator new[] () from /usr/lib/
#4  0x00fef967 in TStat::Median () from
#5  0x00fefb21 in TStat::TukeyBiweight () from

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

Hi Christian,

I would guess that either ‘n’ is crazy large or that she is running out of memory …


Dear Philippe

Although ‘n’ is not so large, she is running out of memory because of the overall size of the data.

The problem is that she had already had problems earlier, where she got the following message:

glibc detected*** /usr/lib/R/bin/exec/R: free(): invalid next size (fast):0x0aa30730 ***

However, in this case I found a memory leak in my program, I forgot to delete two arrays in one function.

Do you think that the current error message could also be caused by a memory leak in a function
which was called earlier in my program?

My problem is that using the identical data I cannot reproduce the error, neither on my Mac with
2GB RAM nor on WinXP with 1GB RAM, while she uses Linux with 2GB RAM.

Best regards

Hi Christian,

It could also be a memory overwrite of some sort (which could be due to the use of un-initialized memory which would explain the different behavior on different platforms).

I would recommend that she runs the program using valgrind which would be able to pin-point memory overwrite and un-initialized memory use problems.


Dear Philippe

Since I have installed openSUSE11.1 in a virtual machine on my Mac, I have finally run the data on Linux
and did also get the memory error. Sadly it also damaged my virtual machine so that I had to rebuild it.

I am currently running valgrind with test data with results that I do not understand. However, since
some memory errors may also involve ROOT I will start a new topic.

Best regards