`Internal error' in vector::sort

Dear CINT experts,

The following snippet of code leads to trouble.


  gROOT->Reset();

  #include <vector>
  #include <algorithm>

  vector<Int_t> a;
  a.push_back(2);
  a.push_back(1);

  cout << "DEBUG before: a = (";
  for (size_t i = 0; i != a.size(); ++i) {
    cout << a.at(i);
    if (i != (a.size()-1))
      cout << ", ";
  } // for (i)
  cout << ")" << endl;

  a.sort(a.begin(), a.end());

  cout << "DEBUG after : a = (";
  for (size_t i = 0; i != a.size(); ++i) {
    cout << a.at(i);
    if (i != (a.size()-1))
      cout << ", ";
  } // for (i)
  cout << ")" << endl;

}

Running on an Intel Mac, OS X 10.5.2, gcc version 4.0.1 (Apple Inc. build 5465), ROOT built for arch `macosx’.

ROOT v5.18.00@21746 with CINT 5.16.29, Jan 08, 2008 tells me:

root [0] 
Processing test_cint_sort.C...
DEBUG before: a = (2, 1)
Internal error: template function call __unguarded_partition failed algo.h:721:
*** Interpreter error recovered ***

ROOT v5.19/03 (trunk@23489) with the same CINT version tells me:

root [0] 
Processing test_cint_sort.C...
DEBUG before: a = (2, 1)
Error: operator- not defined for vector<int,allocator<int> >::iterator algo.h:722:

This puzzles me. How do I get two different errors with the same CINT version? And how should I sort this vector?

Thanks,
Jeroen

[quote=“jhegeman”]Dear CINT experts,

The following snippet of code leads to trouble.


  gROOT->Reset();

  #include <vector>
  #include <algorithm>

  vector<Int_t> a;
  a.push_back(2);
  a.push_back(1);

  cout << "DEBUG before: a = (";
  for (size_t i = 0; i != a.size(); ++i) {
    cout << a.at(i);
    if (i != (a.size()-1))
      cout << ", ";
  } // for (i)
  cout << ")" << endl;

  a.sort(a.begin(), a.end());

  cout << "DEBUG after : a = (";
  for (size_t i = 0; i != a.size(); ++i) {
    cout << a.at(i);
    if (i != (a.size()-1))
      cout << ", ";
  } // for (i)
  cout << ")" << endl;

}

Running on an Intel Mac, OS X 10.5.2, gcc version 4.0.1 (Apple Inc. build 5465), ROOT built for arch `macosx’.

ROOT v5.18.00@21746 with CINT 5.16.29, Jan 08, 2008 tells me:

root [0] 
Processing test_cint_sort.C...
DEBUG before: a = (2, 1)
Internal error: template function call __unguarded_partition failed algo.h:721:
*** Interpreter error recovered ***

ROOT v5.19/03 (trunk@23489) with the same CINT version tells me:

root [0] 
Processing test_cint_sort.C...
DEBUG before: a = (2, 1)
Error: operator- not defined for vector<int,allocator<int> >::iterator algo.h:722:

This puzzles me. How do I get two different errors with the same CINT version? And how should I sort this vector?

Thanks,
Jeroen[/quote]
Since when std::vector has its own sort method?

the following works for me:

[code]#include
#include

void test()
{
vector<Int_t> a;
a.push_back(2);
a.push_back(1);

cout << “DEBUG before: a = (”;
for (size_t i = 0; i != a.size(); ++i) {
cout << a.at(i);
if (i != (a.size()-1))
cout << ", ";
} // for (i)
cout << “)” << endl;

std::sort(a.begin(), a.end());

cout << “DEBUG after : a = (”;
for (size_t i = 0; i != a.size(); ++i) {
cout << a.at(i);
if (i != (a.size()-1))
cout << ", ";
} // for (i)
cout << “)” << endl;
}[/code]

Doh! Too much python I guess.

Thanks!

P.S. Then what does happen when I call vector::sort???

or a bit nicer:

#include <vector>
#include <algorithm>
#include <iterator>
 
typedef vector<Int_t> IntVector_t;

void test()
{
  IntVector_t a;
  a.push_back(2);
  a.push_back(1);

  cout << "DEBUG before: a = ( ";
  copy(a.begin(), a.end(),
       ostream_iterator<IntVector_t::value_type>(cout, " ") );
  cout <<")" << endl;

  std::sort(a.begin(), a.end());

  cout << "DEBUG after: a = ( ";
  copy(a.begin(), a.end(),
       ostream_iterator<IntVector_t::value_type>(cout, " ") );
  cout <<")" << endl;
}

[quote=“jhegeman”]
P.S. Then what does happen when I call vector::sort???[/quote]
a good question. I don’t know. :frowning:
I think CINT experts could have a guess.

Hi,

I getroot [0] vector<int> a root [1] a.sort(a.begin(), a.end()) Error: Can't call vector<int,allocator<int> >::sort(a.begin(),a.end()) in current scope (tmpfile):1: Possible candidates are... *** Interpreter error recovered *** which is what I would expect. That’s on linux GCC 3.4.6 with the current trunk.

Cheers, Axel.

Hi Axel,

Something changed this behaviour indeed between 5.18 and today’s trunk.

I can reproduce your result under OS X as well:

root [0] vector<int> a root [1] a.sort(a.begin(), a.end()) Error: Can't call vector<int,allocator<int> >::sort(a.begin(),a.end()) in current scope (tmpfile):1: Possible candidates are... *** Interpreter error recovered ***

But I still wonder what exactly happens when I include before trying the sort:

root [0] #include <algorithm> root [1] vector<int> a root [2] a.sort(a.begin(), a.end()) Error: operator- not defined for vector<int,allocator<int> >::iterator algo.h:722: *** Interpreter error recovered ***

I know it’s not supposed to work, but what is it trying to do???

Cheers,
Jeroen

Hi Jeroen,

it’s calling the friended function std::sort. That’s of course wrong and we will fix that in the new CINT version, when re-writing the function overload resolution. Thanks for the example!

Cheers, Axel.