Re: Memory Allocation for STL "set"

From: Hendrik Schober (SpamTrap_at_gmx.de)
Date: 03/01/04


Date: Mon, 1 Mar 2004 20:46:49 +0100

JC <anonymous@discussions.microsoft.com> wrote:
> I am having a problem that is most likely arising because I don't
> understand the memory allocation techniques that the STL (in
> MSVC 6.0) uses.
>
> I am using pointers and HeapAlloc() for most of my data structures
> to save execution time by avoiding repetitive memory copies of
> the data structures. I have the following structure defined:
>
> typedef struct _REC
> {
> int value;
> } REC;
>
> typedef struct _NODE
> {
> std::set<REC,COMP_REC> psetRec;
> } NODE;

  Why don't you just write:

    struct REC {
      int value;
    };

    struct NODE {
      std::set<REC,COMP_REC> psetRec;
    };

  This is C++, you don't need to typedef
  struct tags. (And what are these CAPS for?
  This looks like macros to me.)

> (COMP_REC is comparison function defined in general scope
> elsewhere) I create a std::vector of NODE* with:
>
> std::vector<NODE*> vecpNode;

  This is rather dangerous, as this way you
  need to take care of managing the lifetime
  of the 'NODE' objects yourself. If you don't
  want to store 'NODE' objects directly, use
  some start pointer (see www.boost.org) for
  some good ones.
  Also, _why_ do you want to store pointers
  instead of objects? Your above comment
  implies that you want to gain performance
  through this. Note that allocating these
  objects on the heap will most certainly be
  slower than doing this on the stack.

> which is populated with NODE* objects that are allocated as follows:
>
> pNode=(NODE*)HeapAlloc(GetProcessHeap(),0,sizeof(NODE));

  This doesn't create 'NODE' objects. Rather
  than that, it just allocates memory for
  them. In C++, this is the way to create
  objects on the heap:
     pNode = new NODE();
  (And this
     delete pNode;
  is the way to get rid of them later.) If
  for some reason you _really_ need to use
  'HeapAlloc()', you need to either use in-
  place construction or define 'NODE'-
  specific versions of 'new' and 'delete'.
  Refer to a good C++ book for these advanced
  techniques.

> Now when I try to access pNode->psetRec in any way, I get an access violation.

  Of course. It's just a blob of memory since
  no constructors were run.

> Note that I did have this whole thing working when I used instead:
>
> std::vector<NODE> vecNode;

  This is what I would advice anyway. The
  'std::set<>' template only uses a few bytes
  itself, most of its data is allocated on
  the heap (using 'new') anyway. Those few
  bytes are copied easily and this frees you
  from having to handle object-lifetime
  manually.

> Does anyone have the right technique, as I prefer to use the pointer approach.

  Why?

> Thanks

  HTH,

  Schobi

-- 
SpamTrap@gmx.de is never read
I'm Schobi at suespammers dot org
"Sometimes compilers are so much more reasonable than people."
  Scott Meyers


Relevant Pages


Loading