Titlebanner
Home
  About
  Blog
  News
  Contact

CV
  download

Code
  C++ AVL Tree
  C++ HashMap

Articles
  VC6 STL
  VC7 STL
  VC6 debug tips

Links


python powered

xemacs

asd


Home » Articles » C++ standard library tips for VC7

C++ standard library tips for VC7

Introduction

VC7 finally has a good standard library. Here are some gotchas that you might stumble into.

Heap fragmentation

C++ Apps are prone to heap fragmentation if the programmer doesn't take care of it and/or the allocation schemes aren't prepared for c++ allocation requests: i.e. many little allocs and many little deallocs throughout the application invocations. On VC7, several factors multiply:

  • allocation scheme of the VC7 standard library.
  • default heap settings in VC7.

Activate the lfh (low fragmenting heap)

Activate it this way

_set_sbh_threshold(128);

or by doing this

ULONG HeapFragValue = 2;

::HeapSetInformation((HANDLE)_get_heap_handle(),
     HeapCompatibilityInformation, &HeapFragValue,
     sizeof(HeapFragValue));

And you may see significant speedups in you C++ app, if your allocation schemes are a lot of small-object allocations or your std::vector::resize operation is simply not fast enough.

Use a custom allocator

You can use a Custom allocator to tune your allocation schemes. I actually tweaked Nicolai Josuttis Allocator template to use Doug Leas dlmalloc Allocator (i.e. I changed _2_ lines), which speeds up things with native stl significantly (A variation of that allocator, ptmalloc, is used in the glibc).

STLPort is far from dead

Reasons you may want to consider STLPort 5.x:

  • fast strings
  • faster stringstreams.
  • Memory allocation scheme of the standard allocator makes more sense
  • I prefer their hashmap implementation.
  • STL debug mode (Visual studio 2005 enterprise edition now has a similiar thing). Indispensable.