This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

What is the default allocator with 3.4.3?


I am using gcc 3.4.3 on Solaris 2.8, and on Red Hat 9.

I believe that the allocation documentation is out of date; it states that the default allocator uses a pool, when in fact the default allocator is the new_allocator.

After reading libstdc++'s allocator documentation at http://gcc.gnu.org/onlinedocs/libstdc++/20_util/allocator.html I assumed that the default allocator uses a memory pool --- allocator.html states that "it's on by default."

However, I am not seeing the behavior I expect for the attached test program (derived from the allocator documentation). The test program creates a linked list of 100 integers, allows that linked list to fall out of scope, and then creates another linked list of 100 integers. I've overloaded operator new and delete so that I can track their usage.

I expected to see fewer then 200 calls to operator new --- but I do. I expected that setting the environment variable GLIBCXX_FORCE_NEW would change my output --- but it doesn't. It all looks as if the default allocator is really the new_allocator; a brief look at the source code confirms that this is the case.

Switching to the other allocators gives me the results I expect --- fewer then 200 calls to operator new, and setting GLIBCXX_FORCE_NEW changes my program's output.

Have I misconfigured my compiler installation? Is my installation broken? Or is it just that the documentation is out of date?

Robert Zeh
http://home.earthlink.net/~rzeh
#include <iostream>
#include <list>
#include <ext/malloc_allocator.h>
#include <ext/pool_allocator.h>
#include <ext/mt_allocator.h>

using namespace std;

void *operator new (size_t size) {
  cout << "operator new " << size << endl;
  return malloc(size);
}

void operator delete (void *p) {
  cout << "operator delete\n";
  free(p);
}


int main ()
{

  {
    //std::list<int, __gnu_cxx::__mt_alloc<double> > List;    
    //std::list<int, __gnu_cxx::__pool_alloc<int> > List;
    std::list<int> List;
    for ( int i=0; i<10; ++i ) {
      cout << "push_back #1" << endl;
      List.push_back( int() );
      List.erase(List.begin());
    }
  }
  cout << "Cleared\n";
  {
    //std::list<int, __gnu_cxx::__mt_alloc<double> > List;
    //std::list<int, __gnu_cxx::__pool_alloc<int> > List;
    std::list<int> List;
    for ( int i=0; i<10; ++i ) {
      cout << "push_back #2" << endl;
        List.push_back( int() );
    }
  }

    return 0;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]