Whats going onto the stack here?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi all,

Ok, hopefully theres a quick answer to this one. Ive just changed some code
to use a linked list of objects instead of a heap array, however I get a
stack overflow when 2151 of these objects are recursivly deleted. Im not
actually sure how the stack is filling up so quick when all the objects in
the list, I assume, are allocated on the heap with 'new'. Im also guessing
each must create its own stack as i can quite happily create 2 or more lists
of 2150 objects but not one of them can be over that amount.

Anyway if anyone can shed some light on whats actually being put onto the
stack in this case as arrays arnt really a useful option for me, especially
considering this list will always be changing, and shifting big blocks of
memory around as im resizing memory is way too slow.

Heres the structure layout of a node in the link list.

struct NODE
{
110 bytes of other data;

NODE *pPrev;
NODE *pNext;

NODE()
{
pPrev = pNext = NULL;
}

~NODE()
{
pPrev = NULL;
if (pNext)
delete pNext;
pNext = NULL;
}
};

OK, so i now have a root pointer for the list in a structure elsewhere
NODE *pNodeRoot = NULL;

and add to the list using this add function which is a member function of
the structure the root pointer is in

void Add(NODE *pNode)
{
pNode->pNext = pNodeRoot;
if (pNodeRoot)
pNodeRoot->pPrev = pNode;
pNodeRoot = pNode;
}

Ok thats that; now i can happily create 2151 objects like this elsewhere

NODE *pNode = NULL;
for (WORD i = 0; i < 2151; ++i) {
pNode = new NODE();
Add(pNode);
}

Now this is fine no problems when the list is created but as soon as i do

if (pNodeRoot)
delete(pNodeRoot);
pNodeRoot = NULL;

I get stack overflow after its deleted 2150 of the objects. Sigh!.

Im obviously missing some important point about link list sizes based on
what its putting on the stack, but what?
Should I be creating multiple smaller lists rather than one big one?
Is there a fast way to evaluate a best size for a linked list based on whats
in it, or should I allocate a fixed heap array of free NODE pointers and use
that to list all my heap objects, allowing me to avoid recursive delete,
probably cutting down on stack use.

Just looking for the best way to avoid this issue.
Thanks in advance

Callum.


.



Relevant Pages

  • Re: remove all occurence of list
    ... ;;; Recurse, where natural. ... Traversing lists tail-recursively is not only an ... it's about limiting your stack space requirements. ... of reverse, every frame would hold exactly one word plus a return point. ...
    (comp.lang.scheme)
  • Re: Lisp dinamism vs ML safety
    ... No reason to use a generic class when there is already a built-in Stack ... My OCaml solution is not so easy because I don't know how to write ... will not be as elegant in OCaml as it is in Lisp. ... This variable contains a list of lists, ...
    (comp.lang.lisp)
  • Re: Linked List & Dynamic Memory Allocation
    ... You can't write in C or C++ without understanding the role of the stack and the heap. ... Cell * ListHead = NULL; ... Note that while C++ using MFC, STL, boost, etc. make it easy to build link lists, none of ...
    (microsoft.public.vc.mfc)
  • Re: remove all occurence of list
    ... ;;; Recurse, where natural. ... Traversing lists tail-recursively is not only an ... it's about limiting your stack space requirements. ... I note that the reference implementation for SRFI-1 uses the ...
    (comp.lang.scheme)
  • Re: Whats going onto the stack here?
    ... some code to use a linked list of objects instead of a heap array, ... however I get a stack overflow when 2151 of these objects are ... Every function call needs some stack space for return address, ... as i can quite happily create 2 or more lists of 2150 objects but not ...
    (microsoft.public.vc.language)